Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 08-24-2004, 07:20 PM   #1 (permalink)
Everex
Registered User
 
Everex's Avatar
 
Join Date: Aug 2004
Posts: 2
Everex is on a distinguished road
Help Amature Student with his code! please

Well, i am very new to java and am taking a computer science course at my school. for our first project we where asked to create a very simple code that took the users birthdate input (month date, year [format]). Then it took that birthdate and broke it up using strings, displaying the users month of birth date of birth and year of birth into a message box.

My problem is that i got everything to work except for when it shows the user Date of birth, for me it always adds the , year next to the date and everything i have tried yas lead me into complier errors or not done the desired results

well here is my code can anyone help me fix the problem?




Quote:
/* Author: Arsalan Modjbafan
* Due date: 08-25-04
* Lab #: Lab 2A
* File: Birthday.Java
*
* The Lab2A class will bring up an input box that asks for a users birth date
* Then uses that information to show them a message spliting up the Month Day and Year
*/

import javax.swing.*;

public class Birthday {

public static void main( String[] args) {




JFrame lab2A;

lab2A = new JFrame();

lab2A.setSize(700,400);
lab2A.setLocation(324,0);
lab2A.setTitle("Lab 2A");
lab2A.setVisible(true);



String month, day, year, birthday;


birthday = JOptionPane.showInputDialog(lab2A,
"Enter birth date in the format Month Day, Year\nFor example: August 25, 2004");




month = birthday.substring(0, birthday.indexOf(" "));

day = birthday.substring(birthday.indexOf(" ") + 1);

year = birthday.substring(birthday.indexOf(", ") + 1);

JOptionPane.showMessageDialog(lab2A, "Month: " + month + "\nDay: " + day + "\nYear: " + year);


}

}
Everex is offline   Reply With Quote
Old 08-24-2004, 07:48 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,486
sde is on a distinguished road
maybe this code will help:
Code:
        String month, day, year, birthday, buffer;
        
        birthday = "August 25, 2004";

        month = birthday.substring(0, birthday.indexOf(" "));
        System.out.println(month);
        
        buffer = birthday.substring(birthday.indexOf(" ")+1,birthday.length());
        
        day = buffer.substring(0,buffer.indexOf(", "));
        System.out.println(day);
        
        buffer = buffer.substring(buffer.indexOf(", ")+2,buffer.length());

        year = buffer;
        System.out.println(year);
__________________
Mike
sde is offline   Reply With Quote
Old 08-24-2004, 08:07 PM   #3 (permalink)
Everex
Registered User
 
Everex's Avatar
 
Join Date: Aug 2004
Posts: 2
Everex is on a distinguished road
Quote:
Originally posted by sde
maybe this code will help:
Code:
        String month, day, year, birthday, buffer;
        
        birthday = "August 25, 2004";

        month = birthday.substring(0, birthday.indexOf(" "));
        System.out.println(month);
        
        buffer = birthday.substring(birthday.indexOf(" ")+1,birthday.length());
        
        day = buffer.substring(0,buffer.indexOf(", "));
        System.out.println(day);
        
        buffer = buffer.substring(buffer.indexOf(", ")+2,buffer.length());

        year = buffer;
        System.out.println(year);
thanks for the code but it contains codeing that i have not even heard of half of this stuff yet like printin and buffer and stuff so im gonna need to just stick with strings and index of and length


would this work?

Quote:

String month, day, year, birthday;


birthday = JOptionPane.showInputDialog(lab2A,
"Enter birth date in the format Month Day, Year\nFor example: August 25, 2004");



month = birthday.substring(0, birthday.indexOf(" "));

day = birthday.substring(birthday.indexOf(" ")+1, birthday.indexOf(", "));

year = birthday.substring(birthday.indexOf(", ") + 1);

JOptionPane.showMessageDialog(lab2A, "Month: " + month + "\nDay: " + day + "\nYear: " + year);


}

}
Everex is offline   Reply With Quote
Old 08-24-2004, 09:52 PM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,486
sde is on a distinguished road
System.out.println() just prints the output to the output window for debugging purposes. just take those lines out. that is how i tested for accuracy.
__________________
Mike
sde is offline   Reply With Quote
Old 08-25-2004, 03:29 AM   #5 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Look up the definition of String.substring() in the API. That's where your problem is.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Would you mind if there were small flash elements at code newbie? sde Lounge 10 05-29-2004 09:05 PM
Cisco Code breaking sde Code Newbie News 0 05-21-2004 07:10 AM
Microsoft probes Windows code leak redhead Code Newbie News 0 02-13-2004 12:41 AM


All times are GMT -8. The time now is 01:31 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting