Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java .hasNextLine()永远循环_Java_Loops_While Loop_Java.util.scanner - Fatal编程技术网

Java .hasNextLine()永远循环

Java .hasNextLine()永远循环,java,loops,while-loop,java.util.scanner,Java,Loops,While Loop,Java.util.scanner,这个while循环只会永远循环。我查找解决方案并尝试添加一些消耗输入的内容,但是这没有帮助。未打印printf“readDONEZO” 这是我的密码 public void read(Scanner stdin) { int sRow = 0; while ( stdin.hasNextLine() ) { String theLine = stdin.nextLine(); String[] split = theLine.split(",");

这个while循环只会永远循环。我查找解决方案并尝试添加一些消耗输入的内容,但是这没有帮助。未打印printf“readDONEZO”

这是我的密码

public void read(Scanner stdin) {
    int sRow = 0;
    while ( stdin.hasNextLine() ) {
        String theLine = stdin.nextLine();
        String[] split = theLine.split(",");

        int size = split.length; //how many values in array = 3
        for(int i = 0; i < size ; i++){
            String value = split[i];
            int sColumn = i;
            setCellValue(sRow, sColumn, value);
            System.out.printf("%s", getCellValue(sRow,sColumn));
            if (i+1 != size) {
            System.out.printf(",");
            }
        }   
        sRow += 1;
        System.out.printf("\n");
    }
    System.out.printf("readDONEZO\n");
}
阶级

import java.util.Scanner;
公共类电子表格{
私有int行;
私有int列;
私有字符串[][]单元格;
所有行中的私有行;
私有int所有列;
公共字符串电子表格(int行、int列){
allColumns=0;
allRows=0;
this.rows=行;
this.columns=列;
cells=新字符串[this.rows][this.columns];
}
公共无效读取(扫描仪标准){
while(stdin.hasNextLine()){
字符串theLine=stdin.nextLine();
String[]split=line.split(“,”);
allColumns=split.length;//数组中有多少个值=3
for(int i=0;i
代码中的问题是您从未关闭扫描仪,因为stdin总是准备好接收新输入


因此,
stdin.hasNextLine()
while条件始终为true,它使while循环成为无止境的循环。如果将扫描仪(
System.in
)的输入替换为工作站中文件的路径,该示例将正常工作,因为该文件有最后一行。

仅作记录:调用变量stdin有点混乱。或者您认为您真的不想对来自不同来源的扫描仪使用此方法吗?您正在读取的来源是什么?是文件还是系统。在中?我尝试了你的代码,效果很好(我对与单元格相关的两行进行了注释)。有可能看到如何调用该方法吗?如果EOF是一个
空换行符
,当
该行
等于一个
空换行符
import java.io.*;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    int rows = 100;
    int columns = 26;

    StringSpreadsheet a = new StringSpreadsheet(rows, columns);

    Scanner stdin = new Scanner(System.in);
    a.read(stdin);
    System.out.printf("out of read\n");
    a.write();
}   
}
import java.util.Scanner;

public class StringSpreadsheet {

private int rows;
private int columns;
private String[][] cells;
private int allRows;
private int allColumns;

public StringSpreadsheet(int rows, int columns) {
    allColumns = 0;
    allRows = 0;
    this.rows = rows;
    this.columns = columns;
    cells = new String[this.rows][this.columns];
}

public void read(Scanner stdin) {
    while ( stdin.hasNextLine() ) {
        String theLine = stdin.nextLine();
        String[] split = theLine.split(",");

        allColumns = split.length; //how many values in array = 3
        for(int i = 0; i < allColumns ; i++){
            String value = split[i];
            int sColumn = i;
            setCellValue(allRows, sColumn, value);
            System.out.printf("%s", getCellValue(allRows,sColumn));
            if (i+1 != allColumns) {
            System.out.printf(",");
            }
        }   
        allRows += 1;
        System.out.printf("\n");
    }
    System.out.printf("readDONEZO\n");
}



public void write() {                                                           
    for (int i = 0 ; i < allRows ; i++){                                                    
        for(int j = 0 ; j < allColumns ; j++){                                                      
            String value = getCellValue(i, j);
            if ()
            System.out.printf("%s,", value);
        }
    }
}

public String getCellValue(int gRow, int gColumn) {
    return cells[gRow][gColumn];
}

public void setCellValue(int sRow, int sColumn, String value) {
    cells[sRow][sColumn] = value;
}
}