I am trying to populate a 2D array from a text file (which is simply pi to the 900th decimal place, no spaces or anything besides 3.14...) Right now it will print which row and column index I am on, along with a counter (which will be removed eventually).
import java.util.Scanner;
public class Array {
public static void main(String[] args) throws Exception {
// instantiate input file
java.io.File file = new java.io.File("C:\\arrayinput.txt");
// instantiate scanner to read file
Scanner scanner = new Scanner(file);
// instantiate array
String[][] array = new String[20][45];
// instantiate counter
int i = 1;
// loop to read file and assign a character to each array element
for (int rows = 0; rows < array.length; rows++) {
for (int columns = 0; columns < array[rows].length; columns++) {
System.out.println("Row: " + rows + ", column: " + columns + ", counter = " + i);
i++;
array[rows][columns] = scanner.next();
} // end inner for loop
} // end outer for loop
scanner.close();
}// end main method
}
This code will loop twice and display:
Row: 0, column: 0, counter = 1
Row: 0, column: 1, counter = 2
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at Array.main(Array.java:30)
I do not understand why I'm getting this exception, when the code is ran without
array[rows][columns] = scanner.next();
It will loop through rows 0-19, and columns 0-39 as I expect, and the counter will iterate all the way to 900 (since the text file is pi to the 900th place). But when it starts to assign each character from the text file to each element, I get the error thrown when I get to row 0, column 2 and I have no idea why. Contents of my arrayinput.txt file:
3.
Always check if your scanner hasNext()
token and then consider calling next()
. Try this one:
I fixed some errors, my compiler was complaining about an exception that you has not being caught. Finally close the scanner in a finally block (or your try-with-resources).
public static void main(String[] args) {
// instantiate input file
File file = new java.io.File("file.txt");
Scanner scanner = null;
// instantiate scanner to read file
try {
scanner = new Scanner(file);
// instantiate array
String[][] array = new String[20][45];
// instantiate counter
int counter = 1;
// loop to read file and assign a character to each array element
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println("Row: " + i + ", column: " + j + ", counter = " + counter++);
if (scanner.hasNext()) {
array[i][j] = scanner.next();
}
} // end inner for loop
} // end outer for loop
} catch(FileNotFoundException e) {
System.err.println("There is no such file");
}finally{
scanner.close();
}
}
Edit
The output:
Row: 0, column: 0, counter = 1
Row: 0, column: 1, counter = 2
Row: 0, column: 2, counter = 3
Row: 0, column: 3, counter = 4
Row: 0, column: 4, counter = 5
Row: 0, column: 5, counter = 6
Row: 0, column: 6, counter = 7
Row: 0, column: 7, counter = 8
Row: 0, column: 8, counter = 9
Row: 0, column: 9, counter = 10
Row: 0, column: 10, counter = 11
Row: 0, column: 11, counter = 12
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.