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.
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.
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
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
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
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.
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?
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.