• Post Reply Bookmark Topic Watch Topic
  • New Topic

hasnext and nextInt issues, please help me understand the problem  RSS feed

 
Irving Luna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to create a program that receives the dates from file dates.txt and outputs them to dates.out, if the date is valid, then it will provide a new format and the day that the date falls in the year, if not it will return "Invalid date: (original date and format)" it seems that I have it all completed below, but I keep getting the error below when I type in the correct file name, im not sure what the problem is since I'm calling for a valid int.




content of dates.txt file

10/1/1999
12/31/2000
2/29/1900
2/1/1996
1/1/2097
2/29/2000
7/4/1776
5/32/3001
0/2/1234
8/0/2345
9/30/3001
2/29/2010
3/31/2001
13/3/1867
12/31/3000
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by creating a program which does nothing but read from your text file and print it on the screen. See whether you get the same Exception. Remember to use nextInt when you expect an int, etc. The reason is that you want to confirm that the Scanner is having the problem and you still get the InputMismatchException (IME).
I think you will probably suffer the same IME. If you look up IME in the documentation, you find it only occurs when Scanner has a problem. And it is a problem which I think you will not find in the books; if you look up Scanner#nextLine(), it doesn't say “reads the next line”.

More about it, which I hope you can actually understand, in this old post. You may have to use nextLine twice.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!