简体   繁体   English

线程“主”中的Java异常java.util.NoSuchElementException运行时错误

[英]Java Exception in thread “main” java.util.NoSuchElementException runtime error

I have been making a simple java program as a homework assignment for school. 我一直在做一个简单的Java程序作为学校的家庭作业。 When I added a method to retrieve a 2D array from a .txt file this error appeared. 当我添加了一种从.txt文件中检索2D数组的方法时,就会出现此错误。 The program does not show any errors when compiling. 该程序在编译时不显示任何错误。 I am a new programmer so please go through anything added or changed thoroughly. 我是一名新程序员,因此请彻底检查所有添加或更改的内容。 Please feel free to give me further tips and advice other than the question at hand. 除了手头的问题,请随时给我进一步的提示和建议。

Thanks in advance 提前致谢

Here is the code: 这是代码:

import java.util.*;
import java.io.*;

public class simpleAI2
{

   public static void main (String [] args)
   {
      int count = 0;
      String[][] array = new String [20][4];
      simpleAI2.getArrayData(array);
      String leaveQ;
      int rep = 1;

      do
      {
         int countTwo = 0;
         boolean flag = false;
         Scanner scanName = new Scanner (System.in);
         Scanner scanSport = new Scanner (System.in);
         Scanner leave = new Scanner (System.in);


         System.out.println("My name is A.I.S.C.M.B.T. What is your name?");
         array[count][1] = scanName.nextLine ();
         System.out.println("Hi "+array[count][1]+"! What's your favourite sport?");
         array[count][2] = scanSport.nextLine ();
         String sport = array[count][2];

         for(int x = 1;x<rep;x++)
         {
            if(!array[countTwo][2].equals(null) && array[countTwo][2].equals(array[count][2]))
               {
               flag = true;
               x = 28;
               }

            else
               {
               flag = false;
               }

            countTwo ++;
         }


         countTwo --;

         if(flag == true)
            {
            System.out.println("I know "+array[countTwo][2]+". It is "+array[countTwo][3]+". My friend "+array[countTwo][1]+" knows it");
            }

         if(flag == false)
            {
            System.out.println("I don't know "+array[count][2]+". I only know robot boxing. Robots hit each other until one malfunctions. What is this alien sport you speak of?");
            array[count][3] = scanSport.nextLine ();
            }

         System.out.println("Go again? Type no to leave me :(");
         leaveQ = leave.nextLine ();

         rep ++;

         count ++;

         if(leaveQ.equals("no"));
            {
            simpleAI2.Save(array);
            }

      }while (!leaveQ.equals("no"));

   }



   public static void Save(String [][] array){

      try {
         PrintWriter writer = new PrintWriter(new File("arrayData.txt"));

         for(int x=0; x<array.length; x++){
            for(int y=0; y<array[x].length; y++){
               writer.write(String.valueOf(array[x][y]));
            }
            writer.println(); 
         }

         writer.flush();  
         writer.close();        



      } 
      catch (FileNotFoundException e) 
         {      
         e.printStackTrace();
         }


   }

   public static void getArrayData(String [][] array){

      try {
         Scanner scan2 = new Scanner(new File("arrayData.txt"));

         for(int i=0; i<array.length;  i++){
            for(int j=0; j<array[i].length; j++)
            {
               array[i][j]=scan2.next(); 


            }
         }


      } 

      catch (FileNotFoundException e)
         { 
         e.printStackTrace(); 
         }    

   }
}

If you call the Scanner function next() when there is nothing left to read, it will throw a NoSuchElementException . 如果在没有任何要读取的内容时调用Scanner函数next() ,它将抛出NoSuchElementException
Change your code to the following: 将您的代码更改为以下内容:

public static void getArrayData(String [][] array)
{
    try 
    {
        Scanner scan2 = new Scanner(new File("arrayData.txt"));

        for(int i=0; i<array.length;  i++)
        {
            for(int j=0; j<array[i].length; j++)
            {
                if ( ! scan2.hasNext() )  //if there's nothing left to read
                    return;               //exit the function

                array[i][j]=scan2.next(); 
            }
        }
    } 
    catch (FileNotFoundException e)
    { 
        e.printStackTrace(); 
    }    

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM