Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

simpleDateFormat Problems and questions

 
Justin Core
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Justin Core
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 82
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am using the code above without an error.
 
Justin Core
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Justin Core
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IT WORKED !!
You sir are a godsend. As well as everyone else who replied.
Thank you very much.
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pleasure, maybe someone else can explain the whole Scanner and nextLine() issue better though.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic