Java 在文件末尾中断循环
当所有的行都读完后,我试图打破一个循环。我尝试过使用Java 在文件末尾中断循环,java,java.util.scanner,Java,Java.util.scanner,当所有的行都读完后,我试图打破一个循环。我尝试过使用String temp=scan.nextLine,如果temp.equals(“”)break,但这并没有在正确的时间中断循环 我希望它一直读取到文件的末尾,然后退出循环,除了扫描时的。当读取文件中的最后一个值时,hasNextLine()仍然计算为true 代码: 输入: 9 8 0 1 5 6 3 2 输出: reading first line first line boardX: 9 boardY: 8 non smokers po
String temp=scan.nextLine
,如果temp.equals(“”)break
,但这并没有在正确的时间中断循环
我希望它一直读取到文件的末尾,然后退出循环,除了扫描时的。当读取文件中的最后一个值时,hasNextLine()
仍然计算为true
代码:
输入:
9 8
0 1
5 6
3 2
输出:
reading first line
first line
boardX: 9
boardY: 8
non smokers position: 0 1
reading first line
non smokers position: 5 6
reading first line
non smokers position: 3 2
reading first line
Exception in thread "main" java.util.NoSuchElementException
您的while循环已具有以下条件:
while(scan.hasNextLine())
。这意味着继续,直到我们有下一行,如果我们没有退出循环。您的while循环已经有了条件:while(scan.hasNextLine())
。这意味着继续,直到我们有下一行,如果我们没有退出循环。您的while循环已经有了条件:while(scan.hasNextLine())
。这意味着继续,直到我们有下一行,如果我们没有退出循环。您的while循环已经有了条件:while(scan.hasNextLine())
。这意味着继续,直到我们有下一行,如果我们没有退出循环。您可以使用while循环,该循环使用hasNext()
函数。像这样:
while(scan.hasNextLine()) {...}
此外,您还可以检查正在读取的文本文件末尾是否没有多余的行。您还可以尝试使用
.hasNext()
您可以使用使用hasNext()
函数的while循环。像这样:
while(scan.hasNextLine()) {...}
此外,您还可以检查正在读取的文本文件末尾是否没有多余的行。您还可以尝试使用
.hasNext()
您可以使用使用hasNext()
函数的while循环。像这样:
while(scan.hasNextLine()) {...}
此外,您还可以检查正在读取的文本文件末尾是否没有多余的行。您还可以尝试使用
.hasNext()
您可以使用使用hasNext()
函数的while循环。像这样:
while(scan.hasNextLine()) {...}
此外,您还可以检查正在读取的文本文件末尾是否没有多余的行。您也可以尝试一下
.hasNext()
也可以清理代码。打印的步骤不正确,不符合程序逻辑
int line = 0;
while (scan.hasNextLine()){
System.out.println("reading first line");
//means we're reading the board coordinates
if (line == 0){
System.out.println("first line");
boardX = scan.nextInt();
System.out.println("boardX: "+boardX);
boardY = scan.nextInt();
System.out.println("boardY: "+boardY);
} else {
//we're reading the non smokers positions and storing them in an array
int nonSmokersPosX;
int nonSmokersPosY;
nonSmokersPosX = scan.nextInt(); // <-- This is where your code was previously failing.
nonSmokersPosY = scan.nextInt();
System.out.println("non smokers position: "+nonSmokersPosX+" "+nonSmokersPosY);
pairedCoordinates.add(new Pair(nonSmokersPosX, nonSmokersPosY));
}
line++;
}
int行=0;
while(scan.hasNextLine()){
System.out.println(“读取第一行”);
//意味着我们正在读取板坐标
如果(行==0){
System.out.println(“第一行”);
boardX=scan.nextInt();
System.out.println(“boardX:+boardX”);
boardY=scan.nextInt();
System.out.println(“boardY:+boardY”);
}否则{
//我们正在读取非吸烟者的位置并将其存储在一个数组中
国际非吸烟者协会;
不吸烟;
NonsmokersPSX=scan.nextInt();//此外,您可能需要清理代码。打印的步骤不正确,不符合程序逻辑
int line = 0;
while (scan.hasNextLine()){
System.out.println("reading first line");
//means we're reading the board coordinates
if (line == 0){
System.out.println("first line");
boardX = scan.nextInt();
System.out.println("boardX: "+boardX);
boardY = scan.nextInt();
System.out.println("boardY: "+boardY);
} else {
//we're reading the non smokers positions and storing them in an array
int nonSmokersPosX;
int nonSmokersPosY;
nonSmokersPosX = scan.nextInt(); // <-- This is where your code was previously failing.
nonSmokersPosY = scan.nextInt();
System.out.println("non smokers position: "+nonSmokersPosX+" "+nonSmokersPosY);
pairedCoordinates.add(new Pair(nonSmokersPosX, nonSmokersPosY));
}
line++;
}
int行=0;
while(scan.hasNextLine()){
System.out.println(“读取第一行”);
//意味着我们正在读取板坐标
如果(行==0){
System.out.println(“第一行”);
boardX=scan.nextInt();
System.out.println(“boardX:+boardX”);
boardY=scan.nextInt();
System.out.println(“boardY:+boardY”);
}否则{
//我们正在读取非吸烟者的位置并将其存储在一个数组中
国际非吸烟者协会;
不吸烟;
NonsmokersPSX=scan.nextInt();//此外,您可能需要清理代码。打印的步骤不正确,不符合程序逻辑
int line = 0;
while (scan.hasNextLine()){
System.out.println("reading first line");
//means we're reading the board coordinates
if (line == 0){
System.out.println("first line");
boardX = scan.nextInt();
System.out.println("boardX: "+boardX);
boardY = scan.nextInt();
System.out.println("boardY: "+boardY);
} else {
//we're reading the non smokers positions and storing them in an array
int nonSmokersPosX;
int nonSmokersPosY;
nonSmokersPosX = scan.nextInt(); // <-- This is where your code was previously failing.
nonSmokersPosY = scan.nextInt();
System.out.println("non smokers position: "+nonSmokersPosX+" "+nonSmokersPosY);
pairedCoordinates.add(new Pair(nonSmokersPosX, nonSmokersPosY));
}
line++;
}
int行=0;
while(scan.hasNextLine()){
System.out.println(“读取第一行”);
//意味着我们正在读取板坐标
如果(行==0){
System.out.println(“第一行”);
boardX=scan.nextInt();
System.out.println(“boardX:+boardX”);
boardY=scan.nextInt();
System.out.println(“boardY:+boardY”);
}否则{
//我们正在读取非吸烟者的位置并将其存储在一个数组中
国际非吸烟者协会;
不吸烟;
NonsmokersPSX=scan.nextInt();//此外,您可能需要清理代码。打印的步骤不正确,不符合程序逻辑
int line = 0;
while (scan.hasNextLine()){
System.out.println("reading first line");
//means we're reading the board coordinates
if (line == 0){
System.out.println("first line");
boardX = scan.nextInt();
System.out.println("boardX: "+boardX);
boardY = scan.nextInt();
System.out.println("boardY: "+boardY);
} else {
//we're reading the non smokers positions and storing them in an array
int nonSmokersPosX;
int nonSmokersPosY;
nonSmokersPosX = scan.nextInt(); // <-- This is where your code was previously failing.
nonSmokersPosY = scan.nextInt();
System.out.println("non smokers position: "+nonSmokersPosX+" "+nonSmokersPosY);
pairedCoordinates.add(new Pair(nonSmokersPosX, nonSmokersPosY));
}
line++;
}
int行=0;
while(scan.hasNextLine()){
System.out.println(“读取第一行”);
//意味着我们正在读取板坐标
如果(行==0){
System.out.println(“第一行”);
boardX=scan.nextInt();
System.out.println(“boardX:+boardX”);
boardY=scan.nextInt();
System.out.println(“boardY:+boardY”);
}否则{
//我们正在读取非吸烟者的位置并将其存储在一个数组中
国际非吸烟者协会;
不吸烟;
NonsmokersPSX=scan.nextInt();//有两种解决方案。您想在while
条件下检查什么;是至少有一个数字,还是多行
再来一个号码
然后检查是否至少还有一个号码:while(scan.hasNextInt()){
再来一行
然后确保您首先完全读取当前行。扫描仪必须前进到当前行的末尾,然后才能确定是否还有更多行。
因此,作为while
循环中的最后一条语句,将:scan.nextLine();
这两种解决方案中的任何一种都可以使您的代码正确运行。有两种解决方案。在while
条件下,您希望检查哪些内容;是至少有一个数字还是多行
再来一个号码
然后检查是否至少还有一个号码:while(scan.hasNextInt()){
再来一行
那就确保