简体   繁体   中英

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

I have been making a simple java program as a homework assignment for school. When I added a method to retrieve a 2D array from a .txt file this error appeared. 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 .
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(); 
    }    

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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