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 8 of 8

Thread: Help with Scanner class - Reading Data from a file

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

    Default Help with Scanner class - Reading Data from a file

    I am trying to read from a file the following data:

    Joan Smith 1.68 62
    Mary Williams 1.65 78
    Martin Jones 1.83 62
    Henry Davies 1.92 145
    Nigel Brown 1.75 50

    So I wrote the following code:

    public static LinkedList<GymMembers> readFile() throws FileNotFoundException
    {
    LinkedList<GymMembers>gymMem= new LinkedList<GymMembers>();
    Scanner input = new Scanner(new FileReader("Members.txt"));

    input.useDelimiter(" ");


    while(input.hasNext())
    {
    try
    {
    String fName = input.next();
    String lName = input.next();
    double m_height = input.nextDouble();
    int m_weight = input.nextInt();
    gymMem.add(new GymMembers(fName, lName, m_height, m_weight));
    }catch( InputMismatchException e )
    {
    System.out.println("Invalid input entered.. please try again");
    }
    }
    input.close();
    return gymMem;
    }


    The problem is that when is time to read the: double m_height = input.nextDouble();
    an InputMismatchException is thrown but I don't have any idea why that is happening.
    Last edited by billias; June 17th, 2011 at 07:22 AM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    Print out the values of fName and lName to see what is being read.

    Your while loop tests if there is ONE token available (hasNext) but your code tries to read 3 tokens.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    Thank you norm for your help

    I have done what you suggested and I receive the following:
    Joan Smith
    1.68 62
    Mary
    Williams 1.65
    78
    Martin Jones
    1.83 62
    Henry
    Davies 1.92
    145
    Nigel Brown
    1.75 50


    Since you are right how I will manage to read more tokens
    Last edited by billias; June 17th, 2011 at 07:55 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    I don't understand your printout. It shows that the 2 tokens for the names were read and then the double and then the int. All four tokens on the last line were read OK.
    How can you get an exception trying to read a double at this point in reading the input.
    You don't read the double until AFTER reading the two Strings.
    I would expect to see the Strings: fName and lName being printed out and then the error.
    Add an id to your printlns to show which one is being executed:
    System.out.println("lName=" + lName); // show lName with id

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    This is what I get when lName and fName is printed:

    lName=Smith
    fName=Smith
    lName=62
    Mary
    fName=62
    Mary
    lName=1.65
    fName=1.65
    lName=Jones
    fName=Jones
    lName=62
    Henry
    fName=62
    Henry
    lName=1.92
    fName=1.92
    lName=Brown
    fName=Brown
    lName=50

    fName=50

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    Strange - the code you posted should fail when trying to read the first weight value (assuming each gym member is a separate line in the file), because you've set the delimiter to space (instead of the default 'whitespace', which includes newlines and tabs, etc). Unless each line ends with a space, the scanner will try then to read the newline as part of the weight value and complain because it's not numeric. With a space at the end of the line, it will read the following newline as the start of the next fname, which is acceptable (but probably unwanted).

    If you remove the input.useDelimiter(" ") statement, it will revert to the default whitespace delimiter and it should work without problems.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    Better check your input data. The output doesn't look anything like what the input you posted should produce. Something is out of order.
    What statements printed: Mary 2 times without an identifying String with it?
    How did lName get to be 62?

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Scanner class - Reading Data from a file

    Finally solution found using the split() method of the String class (in addition to the Scanner class) as follows:

    public static LinkedList<GymMembers> readFile() throws FileNotFoundException
    {
    LinkedList<GymMembers>gymMem= new LinkedList<GymMembers>();
    Scanner input = new Scanner(new FileReader("Members.txt"));



    try
    {
    //read line by line
    while(input.hasNextLine())
    {
    String line = input.nextLine();
    String[] split= line.split(" ");
    String fName = split[0];
    String lName = split[1];
    double m_height = Double.parseDouble(split[2]);
    int m_weight = Integer.parseInt(split[3]);
    System.out.println( fName + " " + lName + " " + m_height + " " + m_weight);
    gymMem.add(new GymMembers(fName, lName, m_height, m_weight));

    }
    System.out.println();
    }catch( InputMismatchException e )
    {
    System.out.println("Invalid input entered.. please try again");
    }
    input.close();
    return gymMem;
    }

    From the terminal:

    Joan Smith 1.68 62
    Mary Williams 1.65 78
    Martin Jones 1.83 62
    Henry Davies 1.92 145
    Nigel Brown 1.78 50

    I would like to thank you Norm and dlorde for their help.

Similar Threads

  1. Help reading data from file, manipulating then rewriting out.
    By Nismoz3255 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 19th, 2011, 09:13 AM
  2. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM
  3. Reading file Data into a "struct"
    By Brandt in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: February 9th, 2011, 10:02 AM
  4. Basic Java File I/O with Scanner Class Problem
    By miss confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 26th, 2010, 08:04 AM
  5. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM