gpt4 book ai didi

扫描仪上的 Java 问题(未找到线路)我该如何解决这个问题?

转载 作者:行者123 更新时间:2023-12-01 19:43:17 26 4
gpt4 key购买 nike

我一直在做我的第一个java项目,我在扫描仪中遇到了一个错误,我询问用户是否要继续(是或否)我也有同样的错误(没有找到行)在我的查询方法上,但我相信如果我可以修复第一个,另一个也可以修复,我的代码很大,有很多类,但这里是发生错误的主代码:

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

产品:

Exception in thread "main" java.util.NoSuchElementException: No line found 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());

会产生以下错误。

Exception in thread "main" java.io.IOException: Stream closed 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 不可关闭。在 main 中执行第一件事,任何关闭 System.in 的尝试(通过扫描仪或其他方式)都会给出异常。

关于扫描仪上的 Java 问题(未找到线路)我该如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59151646/

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