0

My error message looks like this:

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1540)
    at GPA3.answer(GPA3.java:364)
    at GPA3.main(GPA3.java:355)

I have my main method and the rest of my code working perfectly but once I add this piece of code it throws an error. It does not allow me to enter anything into the Scanner.

import java.util.Scanner;
public class GPA3 {

    public static void main(String[] args) 
    {
        System.out.println("Hello! Welcome to the GPA Calculator.");

        int Math;
        double mathGpa = 0;

        System.out.println("Math = ");
        Scanner input = new Scanner(System.in);
        Math= input.nextInt();

        if (Math >100){
            System.out.println("You have mistyped something");
        }
        if (Math >= 94){
            System.out.println("You have an A");
            mathGpa = 4.0;
            System.out.println(mathGpa);
        }
        if (Math < 94 && Math >=90){
            System.out.println("You have an A-");
            mathGpa = 3.7;
            System.out.println(mathGpa);
        }
        if (Math < 90 && Math >=87){
            System.out.println("You have a B+");
            mathGpa = 3.3;
            System.out.println(mathGpa);
        }
        if (Math < 87 && Math >=80){
            System.out.println("You have a B");
            mathGpa = 3.0;
            System.out.println(mathGpa);
        }
        if (Math < 80 && Math >=77){
            System.out.println("You have a B-");
            mathGpa = 2.7;
            System.out.println(mathGpa);
        }
        if (Math < 77 && Math >=73){
            System.out.println("You have a C+");
            mathGpa = 2.3;
            System.out.println(mathGpa);
        }
        if (Math < 73 && Math >=70){
            System.out.println("You have a C");
            mathGpa = 2.0;
            System.out.println(mathGpa);
        }
        if (Math < 70 && Math >=67){
            System.out.println("You have a C-");
            mathGpa = 1.7;
            System.out.println(mathGpa);
        }
        if (Math < 67 && Math >=67){
            System.out.println("You have a D+");
            mathGpa = 1.3;
            System.out.println(mathGpa);
        }
        if (Math < 67 && Math >=63){
            System.out.println("You have a D");
            mathGpa = 1.0;
            System.out.println(mathGpa);
        }
        if (Math < 63 && Math >=60){
            System.out.println("You have a D-");
            mathGpa = 0.7;
            System.out.println(mathGpa);
        }
        if (Math < 60){
            System.out.println("You have a F");
            mathGpa = 0.0;
            System.out.println(mathGpa);
        }//End of Math

        int English;
        double englishGpa = 0;
        System.out.println("English = ");
        English= input.nextInt();

        if (English >100){
            System.out.println("You have mistyped something");
        }
        if (English >= 94 && English <101){
            System.out.println("You have an A");
            englishGpa = 4.0;
            System.out.println(englishGpa);
        }
        if (English < 94 && English >=90){
            System.out.println("You have an A-");
            englishGpa = 3.7;
            System.out.println(englishGpa);
        }
        if (English < 90 && English >=87){
            System.out.println("You have a B+");
            englishGpa = 3.3;
            System.out.println(englishGpa);
        }
        if (English < 87 && English >=80){
            System.out.println("You have a B");
            englishGpa = 3.0;
            System.out.println(englishGpa);
        }
        if (English < 80 && English >=77){
            System.out.println("You have a B-");
            englishGpa = 2.7;
            System.out.println(englishGpa);
        }
        if (English < 77 && English >=73){
            System.out.println("You have a C+");
            englishGpa = 2.3;
            System.out.println(englishGpa);
        }
        if (English < 73 && English >=70){
            System.out.println("You have a C");
            englishGpa = 2.0;
            System.out.println(englishGpa);
        }
        if (English < 70 && English >=67){
            System.out.println("You have a C-");
            englishGpa = 1.7;
            System.out.println(englishGpa);
        }
        if (English < 67 && English >=67){
            System.out.println("You have a D+");
            englishGpa = 1.3;
            System.out.println(englishGpa);
        }
        if (English < 67 && English >=63){
            System.out.println("You have a D");
            englishGpa = 1.0;
            System.out.println(englishGpa);
        }
        if (English < 63 && English >=60){
            System.out.println("You have a D-");
            englishGpa = 0.7;
            System.out.println(englishGpa);
        }
        if (English < 60){
            System.out.println("You have a F");
            englishGpa = 0.0;
            System.out.println(englishGpa);
        }//End of English

        int Science;
        double SciGpa = 0;
        System.out.println("Science = ");
        Science= input.nextInt();

        if (Science >100){
            System.out.println("You have mistyped something");
        }
        if (Science >= 94 && Science <101){
            System.out.println("You have an A");
            SciGpa = 4.0;
            System.out.println(SciGpa);
        }
        if (Science < 94 && Science >=90){
            System.out.println("You have an A-");
            SciGpa = 3.7;
            System.out.println(SciGpa);
        }
        if (Science < 90 && Science >=87){
            System.out.println("You have a B+");
            SciGpa = 3.3;
            System.out.println(SciGpa);
        }
        if (Science < 87 && Science >=80){
            System.out.println("You have a B");
            SciGpa = 3.0;
            System.out.println(SciGpa);
        }
        if (Science < 80 && Science >=77){
            System.out.println("You have a B-");
            SciGpa = 2.7;
            System.out.println(SciGpa);
        }
        if (Science < 77 && Science >=73){
            System.out.println("You have a C+");
            SciGpa = 2.3;
            System.out.println(SciGpa);
        }
        if (Science < 73 && Science >=70){
            System.out.println("You have a C");
            SciGpa = 2.0;
            System.out.println(SciGpa);
        }
        if (Science < 70 && Science >=67){
            System.out.println("You have a C-");
            SciGpa = 1.7;
            System.out.println(SciGpa);
        }
        if (Science < 67 && Science >=67){
            System.out.println("You have a D+");
            SciGpa = 1.3;
            System.out.println(SciGpa);
        }
        if (Science < 67 && Science >=63){
            System.out.println("You have a D");
            SciGpa = 1.0;
            System.out.println(SciGpa);
        }
        if (Science < 63 && Science >=60){
            System.out.println("You have a D-");
            SciGpa = 0.7;
            System.out.println(SciGpa);
        }
        if (Science < 60){
            System.out.println("You have a F");
            SciGpa = 0.0;
            System.out.println(SciGpa);
        }//End of Science

        int Spanish;
        double SpanGpa = 0;
        System.out.println("Spanish = ");
        Spanish= input.nextInt();

        if (Spanish >100){
            System.out.println("You have mistyped something");
        }
        if (Spanish >= 94 && Spanish <101){
            System.out.println("You have an A");
            SpanGpa = 4.0;
            System.out.println(SpanGpa);
        }
        if (Spanish < 94 && Spanish >=90){
            System.out.println("You have an A-");
            SpanGpa = 3.7;
            System.out.println(SpanGpa);
        }
        if (Spanish < 90 && Spanish >=87){
            System.out.println("You have a B+");
            SpanGpa = 3.3;
            System.out.println(SpanGpa);
        }
        if (Spanish < 87 && Spanish >=80){
            System.out.println("You have a B");
            SpanGpa = 3.0;
            System.out.println(SpanGpa);
        }
        if (Spanish < 80 && Spanish >=77){
            System.out.println("You have a B-");
            SpanGpa = 2.7;
            System.out.println(SpanGpa);
        }
        if (Spanish < 77 && Spanish >=73){
            System.out.println("You have a C+");
            SpanGpa = 2.3;
            System.out.println(SpanGpa);
        }
        if (Spanish < 73 && Spanish >=70){
            System.out.println("You have a C");
            SpanGpa = 2.0;
            System.out.println(SpanGpa);
        }
        if (Spanish < 70 && Spanish >=67){
            System.out.println("You have a C-");
            SpanGpa = 1.7;
            System.out.println(SpanGpa);
        }
        if (Spanish < 67 && Spanish >=67){
            System.out.println("You have a D+");
            SpanGpa = 1.3;
            System.out.println(SpanGpa);
        }
        if (Spanish < 67 && Spanish >=63){
            System.out.println("You have a D");
            SpanGpa = 1.0;
            System.out.println(SpanGpa);
        }
        if (Spanish < 63 && Spanish >=60){
            System.out.println("You have a D-");
            SpanGpa = 0.7;
            System.out.println(SpanGpa);
        }
        if (Spanish < 60){
            System.out.println("You have a F");
            SpanGpa = 0.0;
            System.out.println(SpanGpa);
        }//End of Spanish

        int Religion;
        double RGpa = 0;
        System.out.println("Religion = ");
        Religion= input.nextInt();
        input.close();

        if (Religion >100){
            System.out.println("You have mistyped something");
        }
        if (Religion >= 94 && Religion < 101){
            System.out.println("You have an A");
            RGpa = 4.0;
            System.out.println(RGpa);
        }
        if (Religion < 94 && Religion >=90){
            System.out.println("You have an A-");
            RGpa = 3.7;
            System.out.println(RGpa);
        }
        if (Religion < 90 && Religion >=87){
            System.out.println("You have a B+");
            RGpa = 3.3;
            System.out.println(RGpa);
        }
        if (Religion < 87 && Religion >=80){
            System.out.println("You have a B");
            RGpa = 3.0;
            System.out.println(RGpa);
        }
        if (Religion < 80 && Religion >=77){
            System.out.println("You have a B-");
            RGpa = 2.7;
            System.out.println(RGpa);
        }
        if (Religion < 77 && Religion >=73){
            System.out.println("You have a C+");
            RGpa = 2.3;
            System.out.println(RGpa);
        }
        if (Religion < 73 && Religion >=70){
            System.out.println("You have a C");
            RGpa = 2.0;
            System.out.println(RGpa);
        }
        if (Religion < 70 && Religion >=67){
            System.out.println("You have a C-");
            RGpa = 1.7;
            System.out.println(RGpa);
        }
        if (Religion < 67 && Religion >=67){
            System.out.println("You have a D+");
            RGpa = 1.3;
            System.out.println(RGpa);
        }
        if (Religion < 67 && Religion >=63){
            System.out.println("You have a D");
            RGpa = 1.0;
            System.out.println(RGpa);
        }
        if (Religion < 63 && Religion >=60){
            System.out.println("You have a D-");
            RGpa = 0.7;
            System.out.println(RGpa);
        }
        if (Religion < 60){
            System.out.println("You have a F");
            RGpa = 0.0;
            System.out.println(RGpa);
        }//End of Religion 
        answer();
    }//End of Main

    public static double answer()
        {//Begin answer
        double sClass=0;
        System.out.println("Do you have gym this semester?");
        Scanner input = new Scanner(System.in);
        String yes = "yes";
        String answer = input.nextLine();

        if (answer.equals(yes)){

        }
        input.close();
        return sClass;
    }//End answer

}
CubeJockey
  • 2,209
  • 8
  • 24
  • 31

1 Answers1

0

The issue is that you are trying to open two different Scanner objects on the same stream - System.in - at the same time (both are open simultaneously, which is causing the issue when the second scanner tries to read from the stream.

What you can do is to make your answer() function accept the Scanner object as a parameter and then use it instead of creating a new one on the same stream.

Example -

    .... // Lots of logic till here
    }//End of Religion 
    answer(input);
}//End of Main

public static double answer(Scanner input)
    {//Begin answer
    double sClass=0;
    System.out.println("Do you have gym this semester?");
    String yes = "yes";
    String answer = input.nextLine();

    if (answer.equals(yes)){

    }
    input.close();
    return sClass;
}//End answer

You also need to remove the input.close(); as well, as it will close the underlying stream which is System.in .

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176