![](/img/trans.png)
[英]Reading a txt file using scanner class get InputMismatchException. What am I doing wrong?
[英]Am I using Scanner and File class correctly?
我正在使用java.util.Scanner
和java.io.File
創建一個接受用戶輸入模式的Game of Life程序。
主要問題是我似乎無法獲得程序來讀取pattern.txt
文件...
我沒有看到任何問題,當我編譯它們時, pattern.txt
與.java
和.class
文件位於同一文件夾中。
我是否正確使用File
和Scanner
?
我試圖重新排序import
語句,更改try
& catch
結構,並創建一個新的File(//..Path/../pattern.txt)
以通過相對路徑直接調用該文件
1.txt
是病毒碼文件:
20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
`
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public static void main(String [] args) throws FileNotFoundException{
String inputFileName = "1.txt"; // Directly setting relative path
int [][] initStates = getInitialStates(inputFileName);
Grid gd = new Grid(initStates);
gd.display();
gd.getStats();
gd.run();
}
public static int [][] getInitialStates(String fileName) {
try {
File file = new File(fileName);
// checks to see if file was created
// if (file.exists())
// System.out.println("FILE EXISTS");
// else
// System.out.println("FILENOTFOUDN");
Scanner input = new Scanner(file); // Create scanner to read file
int[][] states = null;
int rows = input.nextInt(); // get rows and cols values from first values of text file
int cols = input.nextInt();
// states = new int[rows][cols]; // Create 2d array w/ rows and cols
states = new int[rows][cols];
// for (int i = 0; i < rows; i++) {
// states[i] = new int[cols];
// }
// Read initial states from the input file
for (int i = 0; i < states.length; i++) {
for (int j = 0; j < states[0].length; j++) {
states[i][j] = input.nextInt();
}
}
return states;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
// return states;
}
`
錯誤輸出是FileNotFoundException打印堆棧跟蹤:
java.io.FileNotFoundException: 1.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.util.Scanner.<init>(Scanner.java:639)
at client.getInitialStates(client.java:67)
at client.main(client.java:24)
Exception in thread "main" java.lang.NullPointerException
at Grid.<init>(Grid.java:14)
at client.main(client.java:26)
我收到NullPointerException,因為getInitialStates()方法未返回2d數組,因為它無法讀取文件。
如果根據注釋拋出FileNotFoundException
,則意味着程序的工作目錄 (即在其中調用java YourMainClassName
目錄)與您嘗試打開的文件的名稱不同。
您可以檢查工作目錄:
System.out.println("Working Directory = " + System.getProperty("user.dir"));
如此答案所述: https : //stackoverflow.com/a/7603444/1083697
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.