Java Problem at scanner (Line not found) How do i fix this?

Gabriel Paes

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

    }
matt

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.

edited at
0

Comments

0 comments
Login to comment

Related

How can I fix no line found error when using scanner in my code?

How do I fix my error with scanner? java.util.InputMismatchException

How do I close a scanner in java?

How do I fix the "Unrecognized field" problem?

How do I fix the problem with the capitalization checker?

How do I fix this problem with While and For loop?

How do i fix it in Java

How do I fix these labels at the end of line?

how do I fix a point on a CSS line

How do I add a blank line after the scanner object?

java scanner.nextLine error: no line found

Java Scanner throws NoSuchElementException: No Line Found

How do I fix this source not found error? Reading text file with buffered reader - Java

How do I fix the "processing-java: command not found" error in the terminal

How do I fix incompatible SQL Server version found?

How Do I Fix "unexpected element <queries> found in <manifest>"?

How do I fix the directory not found for option -F error

How do I fix "positional parameter cannot be found that accepts "+""?

How do I fix 'jekyll: command not found' error permanently?

How do I fix key not found error in python?

How do I fix a memory leak in java

How do I fix this Java notarization error?

Java: How do I fix this NPE?

No line found Scanner

How to fix the Scanner to work inside a while in Java?

Unresolved compilation problem: scanner cannot be resolved. How do I solve this problem?

How do I remove delimiter restovers from a scanner? (Java)

How do I get the Scanner in java to read a string?

How do I use a delimiter with Scanner.useDelimiter in Java?