• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

hasnext and nextInt issues, please help me understand the problem

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79964
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic