Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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.util.Scanner.nextLine(未知源)_Java - Fatal编程技术网

位于java.util.Scanner.nextLine(未知源)

位于java.util.Scanner.nextLine(未知源),java,Java,如何使该值停止超过防止崩溃的实际值 String[][] firstLine = new String[8][10]; Scanner input = new Scanner(new File("lab1.dat")); String temp2 = " "; for(int i = 0; i < firstLine.length; i++){ for(int j = 0; j < firstLine[i].length; j++)

如何使该值停止超过防止崩溃的实际值

    String[][] firstLine = new String[8][10];
    Scanner input = new Scanner(new File("lab1.dat"));
    String temp2 = " ";


    for(int i = 0; i < firstLine.length; i++){
        for(int j = 0; j < firstLine[i].length; j++){
            firstLine[i][j]  = input.nextLine();
            System.out.println(firstLine[i][j] );
查看Scanner方法,并尝试在代码中使用它。 也许是这样:

  String[][] firstLine = new String[8][10];
   Scanner input = new Scanner(new File("lab1.dat"));
    String temp2 = " ";

for(int i = 0; i < firstLine.length; i++){
    for(int j = 0; j < firstLine[i].length; j++){
       if(input.hasNextLine()){
        firstLine[i][j]  = input.nextLine();
        System.out.println(firstLine[i][j] );
       }
String[][]firstLine=新字符串[8][10];
扫描仪输入=新扫描仪(新文件(“lab1.dat”);
字符串temp2=“”;
对于(int i=0;i

或者您可以
尝试catch
那个
nextLine()
方法,并在
catch
块中处理
NoSuchElementException

我想您必须在调用
nextLine
时使用
input.hasNextLine()
,您应该检查扫描仪是否有一个

       try {
            String[][] firstLine = new String[8][10];
            Scanner input = new Scanner(new File("lab1.dat"));
            int count  = 0 ;
            //80 is the number of lines you want to read
             while(input.hasNextLine() && count < 80){ 
                 firstLine[count%8][count%10] = input.nextLine();
                 System.out.println( firstLine[count%8][count%10]);
                 count++;

             }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
试试看{
字符串[][]第一行=新字符串[8][10];
扫描仪输入=新扫描仪(新文件(“lab1.dat”);
整数计数=0;
//80是您要读取的行数
而(input.hasNextLine()&&count<80){
第一行[count%8][count%10]=input.nextLine();
System.out.println(第一行[count%8][count%10]);
计数++;
}
}catch(filenotfounde异常){
e、 printStackTrace();
}

试试ArrayList

Scanner sc = new Scanner(new FileInputStream(new File("Input.txt")));
        ArrayList<List<String>> filedata = new ArrayList<List<String>>();

        while (sc.hasNextLine()) {
            ArrayList<String> row = new ArrayList<String>();
            String[] col = sc.nextLine().split("d*[\\s+]");
            for (int i = 0; i < col.length; i++) {
                row.add(col[i]);
            }
            filedata.add(row);
        }
        System.out.println("File Data : "+filedata);
Scanner sc=new Scanner(新文件inputstream(新文件(“Input.txt”));
ArrayList filedata=新的ArrayList();
while(sc.hasNextLine()){
ArrayList行=新的ArrayList();
字符串[]col=sc.nextLine().split(“d*[\\s+]”);
for(int i=0;i
是什么导致了崩溃(异常)?请提供您的问题的最小运行示例。是否要读取8行表单文件!或以何种方式读取。提供信息?确保您的文件包含8行以上的内容lines@KevinMoore只需编辑question@KevinMoore您可以将数据添加为问题本身的一部分,以便使用代码段op格式化数据部分tion.tip:在
if
中添加
else
子句,并在其上运行
break;
,使循环停止。但是,我认为这不是OP预期的输出。@kevinMoore如果这个答案对您有效,请将其标记为已接受,以帮助其他人找到解决方案