gpt4 book ai didi

扫描文件时出现 java.util.NoSuchElementException

转载 作者:行者123 更新时间:2023-11-30 08:59:03 25 4
gpt4 key购买 nike

我正在读取文件并将所有内容保存到其中。整个程序运行良好,但我没有阅读文件的最后一行,当我尝试这样做时,事情变得很奇怪,文件的最后一行是“END_OF_FILE”。我尝试将它保存为一个字符串,当我收到错误消息时它不起作用,但现在我从我的代码中取出了所有这些并且它没有完全运行程序,它输出的最后一件事是工作是的是我在 main 中的最后一个 for 循环。

这是我的代码:

import java.io.*;

import java.util.Scanner;
public class hurry2 {

public static void main (String [] args) throws IOException
{
File candidates = new File("cipcs115.txt");
Scanner scan = new Scanner(candidates);
Scanner user = new Scanner(System.in);
String state = scan.next();
int numOfCan = scan.nextInt();
String what = scan.nextLine();

String stateWi ;
String stateAl ;
String stateDe ;

int numOfCanWi ;
int numOfCanAl ;
int numOfCanDe ;

Candidate [] candiIl ;
Candidate [] candiWi;
Candidate [] candiAl ;
Candidate [] candiDe;
hurry2 cill = new hurry2();

candiIl = new Candidate[numOfCan];
for ( int i=0; i<numOfCan; i++)
{
String three = scan.next();
String four = scan.next();
String five = scan.next();
int six = scan.nextInt();
double seven = scan.nextDouble();
String eight = scan.nextLine();

Candidate night = new Candidate (three,four,five,six,seven,eight);
candiIl [i] = night ;
System.out.println(night.getName() + " " + night.getMotto());
}

stateWi = scan.next();
numOfCanWi = scan.nextInt();

candiWi = new Candidate[numOfCanWi];
for ( int i=0; i<numOfCanWi; i++)
{
String three = scan.next();
String four = scan.next();
String five = scan.next();
int six = scan.nextInt();
double seven = scan.nextDouble();
String eight = scan.nextLine();

Candidate night = new Candidate (three,four,five,six,seven,eight);
candiWi [i] = night ;
System.out.println(night.getName() + " " + night.getMotto());
}

stateAl = scan.next();
numOfCanAl = scan.nextInt();

candiAl = new Candidate[numOfCanAl];
for ( int i=0; i<numOfCanAl; i++)
{
String three = scan.next();
String four = scan.next();
String five = scan.next();
int six = scan.nextInt();
double seven = scan.nextDouble();
String eight = scan.nextLine();

Candidate night = new Candidate (three,four,five,six,seven,eight);
candiAl [i] = night ;
System.out.println(night.getName() + " " + night.getMotto());
}

stateDe = scan.next();
numOfCanDe = scan.nextInt();

candiDe = new Candidate[numOfCanDe];
for ( int i=0; i<numOfCanAl; i++)
{
String three = scan.next();
String four = scan.next();
String five = scan.next();
int six = scan.nextInt();
double seven = scan.nextDouble();
String eight = scan.nextLine();

Candidate night = new Candidate (three,four,five,six,seven,eight);
candiDe [i] = night ;
System.out.println(night.getName() + " " + night.getMotto());
System.out.println("yes");
}

System.out.println(" what would you like to do? >");
String option = user.next();
switch (option)
{
case "m":
case "M":
cill.menu(candiIl);
break;
}
scan.close();
}

public static void menu(Candidate [] rod)
{
hurry2 cill1 = new hurry2();
Scanner menus = new Scanner(System.in);
System.out.print( "What would you like to do next >");
String option1 = menus.next() ;
switch (option1)
{
case "l" :
case "L" :
cill1.listall(rod);
break ;
}
return ;
}
public static void listall(Candidate [] yes)
{
int numOfCan = 3;
for (int s =0; s<numOfCan;s++)
System.out.println(yes[s].getName() + yes[s].getParty() + yes[s].getMotto());
return ;
}
}

这是我的整个错误消息,这个程序再次输出直到单词 yes 的所有内容,我将其包含在数组 candiDe 的最后一个 for 循环中。在此先感谢您的帮助。

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:907)
at java.util.Scanner.next(Scanner.java:1416)
at hurry2.main(hurry2.java:91)

最佳答案

您似乎正在尝试多次调用 scan.next() 方法,但未检查 scan.hasNext() 方法。

在调用 next() 方法之前尝试使用 scan.hasNext()

关于扫描文件时出现 java.util.NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399675/

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