Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Scanner/FileReader help

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scanner/FileReader help

    This particular part of my project requires me to find an employee ID in an array, then read in the next line as a number of hours. How I'm doing all this doesn't really matter, but when I run the program I get "Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at TestEmp.main(TestEmp.java:107)"

    ...which makes me think that I'm reading through the file wrong.

    	Scanner newfile = new Scanner(new FileReader("Hourly.dat"));
    int search;
    int index;
    while(newfile.hasNext())
    {
      search = newfile.nextInt();
      index = bsearch(empArray, 0, i, search);
      if(empArray[index].getType()== 'H'){
      ((Hourly)empArray[index]).setHours(newfile.nextFloat());
      newfile.nextLine();
      }
    }
    newfile.close();

    and the file I'm reading:
    3212 92.50
    3333 100.00
    3999 85.00
    3112 80.00
    3444 81.00
    3987 90.00
    3564 80.00
    3215 80.00
    3665 87.00
    3985 88.00

    Any ideas to fix this problem would be appreciated.

  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Scanner/FileReader help

    That means you're reading in the wrong type of data for the command you're using.

    For instance, reading in "abcd" and saying

    int n = that value.

    Also, I noticed there's a double variable on the second half of each line. I think it's trying to read that in too.

Similar Threads

  1. FileReader help
    By bibboorton in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 5th, 2010, 07:29 AM
  2. [SOLVED] FileReader.read() reads garbage
    By Hackworth in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: August 27th, 2010, 01:06 PM
  3. [SOLVED] difference between fileReader and bufferReader
    By shadihrr in forum Java Theory & Questions
    Replies: 6
    Last Post: June 8th, 2010, 01:26 AM
  4. FileReader need assistance
    By tazjaime in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 8th, 2009, 01:12 AM
  5. Help With Scanner
    By jtphenom in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 12th, 2009, 08:49 PM