aspose file tools*
The moose likes Beginning Java and the fly likes simpleDateFormat Problems and questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "simpleDateFormat Problems and questions" Watch "simpleDateFormat Problems and questions" New topic
Author

simpleDateFormat Problems and questions

Justin Core
Greenhorn

Joined: Feb 28, 2011
Posts: 10
Ok so here is the deal I am attempting to take data entered from the keyboard in the format MM dd yyyy with the whitespace. Example:12 28 1973



I am becoming very frustrated because I keep receiving this error
Exception in thread "main" java.text.ParseException: Format.parseObject(String) failed
at java.text.Format.parseObject(Format.java:228)
at spacetravel.Main.main(Main.java:51)
Java Result: 1

If someone could help me I would greatly appreciate it. Thank you
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

You should change out the .parseObject() method for just parse(). parse() returns a Date object, parseObject() returns object. Or you could cast the return from parse object.



You're on a gravy train with biscuit wheels Roy.
Justin Core
Greenhorn

Joined: Feb 28, 2011
Posts: 10




Exception in thread "main" java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(DateFormat.java:337)
at spacetravel.Main.main(Main.java:55)
Java Result: 1

When I change it to match my program it does not even let me enter the date but just immediately spits out the error

Any suggestions? Thank you for your reply
Larry Frissell
Ranch Hand

Joined: May 16, 2008
Posts: 82
    
    2

I am using the code above without an error.
Justin Core
Greenhorn

Joined: Feb 28, 2011
Posts: 10
Thank you the above code works for me as well.

Now when I change the code to accept a string from my scanner input.

It yields this error.
Exception in thread "main" java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(DateFormat.java:337)
at spacetravel.Main.main(Main.java:54)
Java Result: 1



Thank you very much for your help. I greatly appreciate it
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You do need to input a valid date in the correct format otherwise it will throw an Exception. And in your case you provided an empty String which is probably caused by just pressing enter instead of entering a date.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Justin Core
Greenhorn

Joined: Feb 28, 2011
Posts: 10
I am not sure I understand what you mean.
I need the scanner to read user input not a predefined string which I specify.
Thank you for your time.
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Exception in thread "main" java.text.ParseException: Unparseable date: ""


This error means that you are passing an empty string to your parser, just as


Exception in thread "main" java.text.ParseException: Unparseable date: "201010101"


Would mean you had given it a garbage date.

Is your scanner working properly?, maybe you can try printing the birthDayString back to yourself after you read it in to confirm that it is as you were expecting.
Justin Core
Greenhorn

Joined: Feb 28, 2011
Posts: 10
I am at a loss here.
To try to see if the scanner is working properly I have edited my program as shown below.
The problem is in the out put it skips over entering the birthday all together The output looks like this

Please enter your name:
Justin c
Please enter your weight in lbs:
134
Please enter your birthdate in the format mm dd yyyy

BUILD SUCCESSFUL (total time: 4 seconds)

Thank you again for all of your help I appreciate it

Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Ok, so you need to change this line



to



I think basically when you use the nextInt() method of scanner and then call the nextLine() method later it's still stuck on the previous line where you typed in the weight.
Justin Core
Greenhorn

Joined: Feb 28, 2011
Posts: 10
IT WORKED !!
You sir are a godsend. As well as everyone else who replied.
Thank you very much.
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33

Pleasure, maybe someone else can explain the whole Scanner and nextLine() issue better though.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Try here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simpleDateFormat Problems and questions