| | |
help!
![]() |
•
•
Join Date: Sep 2008
Posts: 5
Reputation:
Solved Threads: 0
yet again right?
Ok, so this time, the task is to write an application that asks the user for his or her birth date and replies with the day of the week on which they were born.
So here is the code i have.
ok, so when i run it, it comes up with, enter birthday, so i do and hit enter. Then it just reads back what I entered. I need it to say saturday. The TA (who can hardly speak english) said something about using the SimpleDateFormat and creating an object for it, then implementing it, and i thought i had..but i guess not. Any help would be great. Thanks in advanced.
Ok, so this time, the task is to write an application that asks the user for his or her birth date and replies with the day of the week on which they were born.
So here is the code i have.
package Ch2Scanner;
import java.text.SimpleDateFormat;
import java.util.*;
public class Ch2Scanner {
public static void main(String[] args) {
Scanner scanner;
scanner = new Scanner(System.in);
SimpleDateFormat sdf;
sdf = new SimpleDateFormat("EEEE");
String birthday;
//prompt the user for input
java.util.Date bdate = java.sql.Date.valueOf("1990-07-21");
System.out.println("Enter your birthday here:");
birthday= scanner.next();
System.out.println("What is your birthday?" + birthday + "." );
}
}ok, so when i run it, it comes up with, enter birthday, so i do and hit enter. Then it just reads back what I entered. I need it to say saturday. The TA (who can hardly speak english) said something about using the SimpleDateFormat and creating an object for it, then implementing it, and i thought i had..but i guess not. Any help would be great. Thanks in advanced.
If you create an instance of SimpleDateFormat using a pattern that corresponds to your intput, you can use the parse(String) method to convert the string input to a Date object, which you can then use to get the day of the week. Calendar is what you should really use for this, but I'm not sure what the specifics of your assignment are (and the whole date/calendar thing is pretty much a mess anyway).
With this: java.util.Date bdate = java.sql.Date.valueOf("1990-07-21");
you can get the value of a string, so get the value of the input from the user.
Then you can use this value to get the day (represented as an integer):
bdate.getDate() .
Thats pretty much it, if you want the output to be a little better create an array of strings, like: String days[] = {"Sunday", "Monday" etc..}
and then get this day with: days[bdate.getDays]
However, getDate() is deprecated so you might want to look up how to solve the problem with Calendar instead.
Hope this helps!
you can get the value of a string, so get the value of the input from the user.
Then you can use this value to get the day (represented as an integer):
bdate.getDate() .
Thats pretty much it, if you want the output to be a little better create an array of strings, like: String days[] = {"Sunday", "Monday" etc..}
and then get this day with: days[bdate.getDays]
However, getDate() is deprecated so you might want to look up how to solve the problem with Calendar instead.
Hope this helps!
![]() |
Other Threads in the Java Forum
- Previous Thread: how to use scanner correctly
- Next Thread: Need help!
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api applet application array automation binary block bluetooth button character class client code component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac map method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql swing system test textfields threads time title tree tutorial-sample ubuntu update windows working






