help!

Reply

Join Date: Sep 2008
Posts: 5
Reputation: stilldancin12 is an unknown quantity at this point 
Solved Threads: 0
stilldancin12 stilldancin12 is offline Offline
Newbie Poster

help!

 
0
  #1
Sep 25th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,354
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 500
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: help!

 
1
  #2
Sep 25th, 2008
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).
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 53
Reputation: di2daer is an unknown quantity at this point 
Solved Threads: 12
di2daer's Avatar
di2daer di2daer is offline Offline
Junior Poster in Training

Re: help!

 
0
  #3
Sep 25th, 2008
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC