Reputation: 11
I need to make a program that stores values from a text file into multiple arrays. The text file looks like this:
1995 Jun 987 65 Allison
1995 Jul 973 85 Erin
1995 Aug 929 120 Felix
1995 Aug 968 95 Humberto
1995 Aug 965 95 Iris
I need to add each column into a separate array. How do I tell the program to specifically target one column to add it to the array, then the next?
I tried to just do it like this:
File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);
while (inFile.hasNextLine()) {
index++;
inFile.nextLine();
}
hurricaneNames = new String [index];
years = new int [index];
months = new String [index];
pressures = new int [index];
windSpeeds = new int [index];
while (inFile.hasNext()) {
int i = 0;
years [i] = inFile.nextInt();
months [i] = inFile.next();
pressures [i] = inFile.nextInt();
windSpeeds [i] = inFile.nextInt();
hurricaneNames [i] = inFile.next();
System.out.println(years[i]); //print statement to test
i++;
}
But the print statement doesn't print anything.
Upvotes: 0
Views: 67
Reputation: 6123
Your first loop just counts the lines in the file. Then your second loop reads the data, but by then the file is at the end so there is nothing to read.
Let me make a couple of suggestions. First, calling the number of lines index
is misleading. That variable is never used as an index, but only as an array size. Call it lineCount
instead.
Secondly, you can get the number of lines in the file with much simpler code using Java NIO and Streams.
The rest of your code can stay mostly the same. The index variable i
needs to be outside the while
loop. And preferably wrap the Scanner
in a try-with-resources loop.
final Path filePath = Paths.get("hurricanedata.txt");
final int lineCount;
try (Stream<String> lines = Files.lines(filePath)) {
lineCount = (int)lines.count();
}
hurricaneNames = new String[lineCount];
years = new int[lineCount];
months = new String[lineCount];
pressures = new int[lineCount];
windSpeeds = new int[lineCount];
try (Scanner inFile = new Scanner(filePath)) {
int i = 0;
while (inFile.hasNext()) {
years[i] = inFile.nextInt();
months[i] = inFile.next();
pressures[i] = inFile.nextInt();
windSpeeds[i] = inFile.nextInt();
hurricaneNames[i] = inFile.next();
System.out.println(years[i]); //print statement to test
i++;
}
}
Upvotes: 0
Reputation: 11686
In the first while loop, your read the till the end of file. So, in second while loop, you have nothing to read as the pointer is on end of file.
You can do this in one loop only like below:
File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);
List<String> hurricaneNames = new ArrayList<>();
List<Integer> years = new ArrayList<>();
List<String> months = new ArrayList<>();
List<Integer> pressures = new ArrayList<>();
List<Integer> windSpeeds = new ArrayList<>();
while (inFile.hasNextLine()) {
String[] tempArray = inFile.nextLine().split(" ");
years.add(Integer.parseInt(tempArray[0]));
months.add(tempArray[1]);
pressures.add(Integer.parseInt(tempArray[2]));
windSpeeds.add(Integer.parseInt(tempArray[3]));
hurricaneNames.add(tempArray[4]);
}
Upvotes: 1
Reputation: 1384
In your first loop you tested inFile.nextLine()
. So, when you're using your second loop inFile.nextLine()
is exhausted.
Upvotes: 0
Reputation: 661
After you do this:
while (inFile.hasNextLine()) {
index++;
inFile.nextLine(); // HERE
}
your file is now being read at the end of the file. So next() won't start at the beginning of the file like you want it to.
You could simply reopen the file with the scanner:
File fileName = new File ("hurricanedata.txt");
Scanner inFile = new Scanner(fileName);
while (inFile.hasNextLine()) {
index++;
inFile.nextLine(); // Increments the line of the file that you're reading, so the
// next "next()" you call starts at the >next< line of the file.
}
inFile.close();
inFile = new Scanner(fileName); // NOTE: Restarts the Scanner, ensuring that it
// starts reading from the beginning of the file again.
hurricaneNames = new String [index];
years = new int [index];
months = new String [index];
pressures = new int [index];
windSpeeds = new int [index];
int i = 0; // NOTE: This should be OUT of the loop, so that i increments
// and doesn't restart to 0.
while (inFile.hasNext()) {
years [i] = inFile.nextInt();
months [i] = inFile.next();
pressures [i] = inFile.nextInt();
windSpeeds [i] = inFile.nextInt();
hurricaneNames [i] = inFile.next();
System.out.println(years[i]); //print statement to test
i++;
}
Upvotes: 1