gpt4 book ai didi

线程中的 Java 异常 "main"java.util.NoSuchElementException 运行时错误

转载 作者:行者123 更新时间:2023-12-01 11:55:06 25 4
gpt4 key购买 nike

我一直在制作一个简单的java程序作为学校的家庭作业。当我添加从 .txt 文件检索二维数组的方法时,出现了此错误。该程序在编译时不会显示任何错误。我是一名新程序员,因此请彻底检查所有添加或更改的内容。除了手头的问题之外,请随时给我进一步的提示和建议。

提前致谢

这是代码:

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();
}

}
}

最佳答案

如果调用Scanner函数next()当没有任何内容可供读取时,它将抛出 NoSuchElementException
将您的代码更改为以下内容:

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();
}

}

关于线程中的 Java 异常 "main"java.util.NoSuchElementException 运行时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28518498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com