从文件重定向输入时出错
这是我的代码:
import java.util.Scanner;
class Graph{
boolean [][]array;
int N;
Graph (){
array = new boolean [1001][1001];
N=0;
}
void read_graph() {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
sc.nextLine();
String str;
String []substr;
for (int K=1; K<=N; K++){
str = sc.nextLine();
substr = str.split(" ");
for (int I=0; I<substr.length; I++)
System.out.println(substr[0]+" "+substr[I]);
}
}
void query(){
Scanner sc = new Scanner(System.in);
int P, Q;
int counter = 0;
boolean flag = true;
while (flag){
counter++;
P = sc.nextInt();
Q = sc.nextInt();
sc.nextLine();
if ( P == Q && P == 0 )
flag =false;
else {
if (Q == 1)
System.out.println("DFS done");
else
System.out.println("Bfs done");
}
}
}
}
class demo{
public static void main( String [] args ){
Graph G= new Graph();
Scanner sc = new Scanner(System.in);
int numGraphs = sc.nextInt();
while (numGraphs>0){
G.read_graph();
G.query();
numGraphs--;
}
}
}
这是输入数据:
1
6
1 2 3 4
2 2 3 6
3 2 1 2
4 1 1
5 0
6 1 2
5 1
1 0
1 0
0 0
当我用键盘提供此输入数据时,它工作正常,但是当我将此输入保存到文件并将其重定向为输入(在linux中使用“<”)时,它会抛出错误消息。
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:855)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at Graph.read_graph(b.java:13)
at demo.main(b.java:56)
帮我指出错误。
Here's my code:
import java.util.Scanner;
class Graph{
boolean [][]array;
int N;
Graph (){
array = new boolean [1001][1001];
N=0;
}
void read_graph() {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
sc.nextLine();
String str;
String []substr;
for (int K=1; K<=N; K++){
str = sc.nextLine();
substr = str.split(" ");
for (int I=0; I<substr.length; I++)
System.out.println(substr[0]+" "+substr[I]);
}
}
void query(){
Scanner sc = new Scanner(System.in);
int P, Q;
int counter = 0;
boolean flag = true;
while (flag){
counter++;
P = sc.nextInt();
Q = sc.nextInt();
sc.nextLine();
if ( P == Q && P == 0 )
flag =false;
else {
if (Q == 1)
System.out.println("DFS done");
else
System.out.println("Bfs done");
}
}
}
}
class demo{
public static void main( String [] args ){
Graph G= new Graph();
Scanner sc = new Scanner(System.in);
int numGraphs = sc.nextInt();
while (numGraphs>0){
G.read_graph();
G.query();
numGraphs--;
}
}
}
Here's the Input data:
1
6
1 2 3 4
2 2 3 6
3 2 1 2
4 1 1
5 0
6 1 2
5 1
1 0
1 0
0 0
When I give this input data with keyboard it works fine but when I saved this input to file and redirected this as input(in linux using '<'), it throws error message.
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:855)
at java.util.Scanner.next(Scanner.java:1478)
at java.util.Scanner.nextInt(Scanner.java:2108)
at java.util.Scanner.nextInt(Scanner.java:2067)
at Graph.read_graph(b.java:13)
at demo.main(b.java:56)
Help me in pointing out the mistake.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要在每个方法中都创建 Scanner 对象。传递您创建的第一个 Scanner 对象。
以下是应该解决该问题的更改列表:
Don't create a Scanner object in every method. Pass the first Scanner object you have created around.
Here is a list of changes that should fix the issue:
为什么要创建 3 个扫描仪?可能在
1)
P = sc.nextInt();
行中感到窒息
2)
Q = sc.nextInt();
因为在第 1 行中读取只有 1 个 int 的输入,然后第 2 行尝试扫描 nextInt() 中的空行。
我不知道为什么这在手动输入时会起作用,除非输入的顺序不同。
Why are you creating 3 scanners? It is possible that it is choking in the lines
1)
P = sc.nextInt();
2)
Q = sc.nextInt();
because the input with only 1 int is being read in line 1 and then line 2 is trying to scan the nextInt() for an empty line.
I have no idea why this would work when inputing by hand, unless the input is in a different order.
您不应使用
<
来重定向输入。您需要使用扫描仪类来读取文件。You should not use
<
to redirect input. You need to use scanner class to read from file.