|
 |
|
 |
08-24-2004, 07:20 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 2
|
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);
}
}
|
|
|
|
08-24-2004, 07:48 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,486
|
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
|
|
|
08-24-2004, 08:07 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Aug 2004
Posts: 2
|
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);
}
}
|
|
|
|
08-24-2004, 09:52 PM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,486
|
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
|
|
|
08-25-2004, 03:29 AM
|
#5 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,161
|
Look up the definition of String.substring() in the API. That's where your problem is.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 01:31 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|