扫描仪 Java 刚刚读取第一行
Scanner Java just reading the first line
我有一个这样的文本文件:
1 2
3 7
5 8
每行有 2 个数字。我想对第一个数字和第二个数字做一些不同的事情。我正在尝试扫描文本文件并打印数字以确保我扫描正确。但是,只有前两个数字出现 (1 4),然后出现错误:
"java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:858)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextInt(Scanner.java:2161)
at java.base/java.util.Scanner.nextInt(Scanner.java:2115)
at com.company.SCC.input(SCC.java:30)
at com.company.SCC.<init>(SCC.java:15)
at com.company.Main.main(Main.java:11)"
我不明白问题出在哪里以及如何逐行扫描文档(我回收了扫描仪的代码,它以前可以工作)。我不确定我做错了什么,任何帮助将不胜感激。
try {
String file = "testcase1.txt";
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String s;
int x;
while ((s = br.readLine()) != null) {
Scanner sca = new Scanner(s);
x = sca.nextInt();
graph.addVertex(x);
int y = sca.nextInt();
graph.addAdjvex(x, y);
System.out.println(x + " " + y);
}
} catch (Exception e) {
e.printStackTrace();
}
也许你应该调用sc.nextLine()跳到下一行
尝试
try {
File file = new File("testcase1.txt");
Scanner sc = new Scanner(file);
int x, y;
while (sc.hasNextLine()) {
x = sc.nextInt();
y = sc.nextInt()
graph.addVertex(x);
graph.addAdjvex(x, y);
System.out.println(x + " " + y);
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
File file = new File("testcase1.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
int x = sc.nextInt();
int y = sc.nextInt();
sc.nextLine();
graph.addVertex(x);
graph.addAdjvex(x, y);
System.out.println(x + " " + y);
}
sc.close()
} catch (Exception e) {
e.printStackTrace();
}
我有一个这样的文本文件:
1 2
3 7
5 8
每行有 2 个数字。我想对第一个数字和第二个数字做一些不同的事情。我正在尝试扫描文本文件并打印数字以确保我扫描正确。但是,只有前两个数字出现 (1 4),然后出现错误:
"java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:858)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextInt(Scanner.java:2161)
at java.base/java.util.Scanner.nextInt(Scanner.java:2115)
at com.company.SCC.input(SCC.java:30)
at com.company.SCC.<init>(SCC.java:15)
at com.company.Main.main(Main.java:11)"
我不明白问题出在哪里以及如何逐行扫描文档(我回收了扫描仪的代码,它以前可以工作)。我不确定我做错了什么,任何帮助将不胜感激。
try {
String file = "testcase1.txt";
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String s;
int x;
while ((s = br.readLine()) != null) {
Scanner sca = new Scanner(s);
x = sca.nextInt();
graph.addVertex(x);
int y = sca.nextInt();
graph.addAdjvex(x, y);
System.out.println(x + " " + y);
}
} catch (Exception e) {
e.printStackTrace();
}
也许你应该调用sc.nextLine()跳到下一行
尝试
try {
File file = new File("testcase1.txt");
Scanner sc = new Scanner(file);
int x, y;
while (sc.hasNextLine()) {
x = sc.nextInt();
y = sc.nextInt()
graph.addVertex(x);
graph.addAdjvex(x, y);
System.out.println(x + " " + y);
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
File file = new File("testcase1.txt");
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
int x = sc.nextInt();
int y = sc.nextInt();
sc.nextLine();
graph.addVertex(x);
graph.addAdjvex(x, y);
System.out.println(x + " " + y);
}
sc.close()
} catch (Exception e) {
e.printStackTrace();
}