I have been doing one of my first java projects and i stuck at a error in a scanner where i ask the user if he wants to continue or not (Y or N) i also have the same error (no line found) on my Query method, but i believe if i can fix the first one the other one can be fixed, my code is big with many classes, but here is the maincode where the error is occuring:
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)
Query:
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);
}
}
}
Output:
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);
}
You haven't provided enough code to reproduce the problem.
When you create a scanner with System.in, then scanner.readLine() will block until a newline has been entered. The only condition that newLine will fail is if the scanner has reached the end of input.
If you do.
Scanner in = new Scanner(System.in);
System.out.println("waiting");
in.nextLine();
System.out.println("enter was pressed");
Everything should work as expected. Ways that we can get an error.
Scanner in = new Scanner(System.in);
System.out.println("waiting");
System.in.close();
in.nextLine();
Produces:
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)
We can check if System.in has been closed by calling System.in.available();
Scanner in = new Scanner(System.in);
System.out.println("waiting");
System.in.close();
System.out.println("System.in.available(): " + System.in.available());
Will produce the following error.
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)
If you are having trouble trying to find out where System.in is getting closed.
System.setIn(new FilterInputStream(System.in){
@Override
public void close(){
throw new RuntimeException("System.in closed!");
}
} );
That will make System.in non-closable. Do that first thing in main, and any attempt to close System.in (via a scanner or otherwise) will give an exception.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments