Reputation: 11
I'm working on a program for my class that asks for me to read data from a txt file, then display it back. I have created a while loop that reads for hasNext, but I'm running into an exception. It says it's a "no such element exception" and I have no idea why. The loop works for 5 iterations, then gives me the exception
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
public class Project2_William_Walker
{
public static void main (String[]args) throws IOException
{
File file = new File ("boarding.txt");
Scanner inputFile = new Scanner(file);
DecimalFormat df = new DecimalFormat("0.00");
String title = "Madison Kennel & Grooming";
String fName;
String lName;
String breed;
String weight;
String age;
double highRisk = 20.00;
while (inputFile.hasNext())
{
fName = inputFile.nextLine();
lName = inputFile.nextLine();
breed = inputFile.nextLine();
weight = inputFile.nextLine();
age = inputFile.nextLine();
inputFile.nextLine();
System.out.println(fName + " " + lName + " " + breed + " " + weight + " " + age);
System.out.println();
}
inputFile.close();
System.out.println("End of Program");
}
}
So in my txt file I have a list that has a first name, then a last name, followed by the breed, the weight and then the age. Looks like this:
Will
Walker
Pug
3
4
John
Appleseed
Retriever
15
7
..and so forth for 4 more entries. My loop gets through the first 5 just fine, but then poops out on the 6th and final time through. Thanks for any help!
Upvotes: 1
Views: 86
Reputation: 17567
Let's find the source of your problem. To easily do that, let's "map" your nextLine()
calls to the file content:
Iteration Code Read line from file
1 fName = inputFile.nextLine(); Will
1 lName = inputFile.nextLine(); Walker
1 breed = inputFile.nextLine(); Pug
1 weight = inputFile.nextLine(); 3
1 age = inputFile.nextLine(); 4
1 inputFile.nextLine(); (empty line between blocks)
2 fName = inputFile.nextLine(); John
2 lName = inputFile.nextLine(); Appleseed
2 breed = inputFile.nextLine(); Retriever
2 weight = inputFile.nextLine(); 15
2 age = inputFile.nextLine(); 7
2 inputFile.nextLine();
As you can see, the last statement inputFile.nextLine()
tries to read a line from a file, but there is nothing more to read anymore, because "7" was the last line. So your problem is that you expect that there is always an empty line after a block.
A quick fix would be adding a check, like this:
if (inputFile.hasNextLine())
inputFile.nextLine();
But this can cause trouble if there is no empty line to split two blocks and the next block starts right after the last one.
To avoid such problems in the first place, I would recommond a different file structure. You can for example use a csv with a "," as the delimiter:
Will,Walker,Pug,3,4
John,Appleseed,Retriever,15,7
And read it like this:
final String line = inputFile.nextLine(); // read a single line
final String[] items = line.split(","); // split that line using ','
fName = items[0];
lName = items[1];
breed = items[2];
weight = items[3];
age = items[4];
System.out.println(fName + " " + lName + " " + breed + " " + weight + " " + age);
And to make it even better: create a model class which holds the necessary fields to represent such a line (or a block of your former file style) and let a csv reader like Jackson do the parsing for you.
Upvotes: 1
Reputation: 424983
You are reading 6 lines in the loop, but the loop condition only checks if there is (at least) 1 line to read.
You probably have an extra blank line at the end of the input file, which causes hasNext()
to return true
after reading in the last block of data.
You would be better to use a for
loop the iterates 5 times, each time checking if there's a line available, and each time saving the data into an array.
Something like:
String[] data = new String[5];
while (inputFile.hasNext()) {
Arrays.fill(data, null); // clear out array, in case input is short
for (int i = 0; i < 5 && inputFile.hasNext(); i++)
data[i] = inputFile.nextLine();
if (inputFile.hasNext())
inputFile.nextLine();
System.out.println(data[0] + " " + data[1] + " " + data[2] + " " + data[3] + " " + data[4]);
System.out.println();
}
inputFile.close();
Upvotes: 1