扫描程序的 Java 问题(找不到行)如何解决此问题?



我一直在做我的第一个 Java 项目之一,我卡在扫描程序中的一个错误,我问用户是否要继续(Y 或 N(我在 Query 方法上也有相同的错误(未找到行(,但我相信如果我能修复第一个,另一个可以修复, 我的代码很大,有很多类,但这里是发生错误的主代码:

public static void main(String[] args) throws IOException {
String tmp = "";
String judgment = ""; // to judge whether running or stopping
Scanner in = new Scanner(System.in);
int num = 0; // counter
do{
System.out.println("Enter the database filename (with file type): ");
tmp = in.nextLine();
fileInput(tmp,num);
++ num;
System.out.printf("Do you have another file? Press "Y" for Yes and "N" for No.n");
judgment = in.nextLine();
}while(judgment.equals("Y") || judgment.equals("y"));
Output();
// ask for query
System.out.println("Do you want to query ? (Y for yes/ N for exit)");
//  Scanner scan = new Scanner(System.in);
judgment = in.nextLine();
// judgment = "Y"; // set judgment manually
while(judgment.equals("Y")||judgment.equals("y"))
Query();
}
Error:
Do you want to query ? (Y for yes/ N for exit)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at GradeSystem.main(GradeSystem.java:259)

查询:

public static void Query() throws IOException {

System.out.println("Please input the student ID: ");
Scanner in = new Scanner(System.in);
String ID = "1109853A-I011-0022"; // inputing id manually if i read next line gives me the error of no next line
for(int i = 0; i < stu.size(); ++i){
if(stu.elementAt(i).stdid.equals(ID)){
for(int j = 0; j < stu.elementAt(i).CourseName.length; ++j){
if(stu.elementAt(i).CourseName[j].equals(""))  {continue;}
else{
System.out.println(stu.elementAt(i).CourseName[j]);
}
}
stu.elementAt(i).GPA = stu.elementAt(i).computeGPA(stu.elementAt(i));
System.out.println("GPA: " + stu.elementAt(i).GPA);

}
}
}

输出:

public static void Output() throws IOException{
System.out.printf("What course you want to display?(Insert Course Code):");
Scanner in = new Scanner(System.in);
String courseName = in.nextLine();
String[] sr = reader(courseName +".txt");
student[] std = new student[sr.length-2];
course cor = new course();
cor.dataC = sr[0]; //maybe on top
cor.CourseInfo(cor.dataC); //Gets name and credits
//   cor.dataC = sr[0]; //maybe on top
cor.studentsnum = Integer.parseInt(sr[1]); // This method will read the number of students

for(int counter = 0; counter < std.length; ++counter) {
std[counter] = new student();
std[counter].dataS = sr[counter+2];
std[counter].StudentInfo(std[counter].dataS); // sets surname,given name, id and score
// while inside the loop it will now check for Highest and lowest score
if(std[counter].score < course.Low) {
course.Low = std[counter].score;
}
if(std[counter].score > course.High) {
course.High = std[counter].score;
}
//*********************************************************************
// Still inside we store the information to do a Average Score:
course.Average += std[counter].score;
}
//Outside the loop gets the average
course.Average /= std.length;
//display
display(Sort.sortField(std),cor);
}

您没有提供足够的代码来重现问题。

当您使用 System.in 创建扫描仪时,scanner.readLine(( 将阻止,直到输入换行符。newLine 失败的唯一条件是扫描程序已到达输入末尾。

如果你这样做。

Scanner in = new Scanner(System.in);
System.out.println("waiting");
in.nextLine();
System.out.println("enter was pressed");

一切都应该按预期工作。我们可以得到错误的方式。

Scanner in = new Scanner(System.in);
System.out.println("waiting");
System.in.close();
in.nextLine();

生产:

线程"main"中的异常 java.util.NoSuchElementException: 找不到行 at java.base/java.util.Scanner.nextLine(Scanner.java:1651( at Junk.main(Junk.java:10(

我们可以通过调用 System.in.available(( 来检查 System.in 是否已关闭;

Scanner in = new Scanner(System.in);
System.out.println("waiting");
System.in.close();
System.out.println("System.in.available(): " + System.in.available());

将产生以下错误。

线程"main"java.io.IOException中的异常:流关闭 at java.base/java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:165( at java.base/java.io.BufferedInputStream.available(BufferedInputStream.java:416( at Junk.main(Junk.java:10(

如果您在尝试找出 System.in 关闭的位置时遇到问题。

System.setIn(new FilterInputStream(System.in){
@Override
public void close(){
throw new RuntimeException("System.in closed!");
}
} );

这将使 System.in 不可关闭。首先要做第一件事,任何关闭 System.in(通过扫描仪或其他方式(的尝试都会给出异常。

最新更新