我正在从一个文件中读取并将所有内容保存到其中。整个程序运行正常,但我没有读到文件的最后一行,当我尝试这样做的时候,事情变得奇怪,文件的最后一行是“END_OF_FILE”。我尝试将其保存为字符串,但是当我收到错误消息时它没有用,但是现在我从我的代码中取出了所有这些并且它没有完全运行程序,它输出的最后一件事是工作是是在我的最后一个循环主要。
这是我的代码:
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,我在最后一个for循环中包含了数组candiDe。在此先感谢帮助人员。
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)
答案 0 :(得分:2)
您似乎在尝试进行多个scan.next()
方法调用而不检查scan.hasNext()
方法。
在致电scan.hasNext()
方法之前尝试next()
。
答案 1 :(得分:0)
它输出的最后一件事是工作是
似乎循环进入,打印yes
并在下一次迭代中抛出异常。
在某个瞬间,你会拿出令牌进入扫描仪。