-3

Following programmingbydoing and got stuck. I get this stacktrace:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at Doing.LessonSeventeen.main(LessonSeventeen.java:23)

Also on line 8 it says "Resource leak 'keyboard' is never closed".

package Doing;

import java.util.Scanner;

public class LessonSeventeen {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in); // <- line 8
        String FirstName, LastName, LoginName;
        int Grade, IDNum;
        double GPA;

        System.out.println("What is your first name?");
        FirstName = keyboard.next();
        System.out.println("What is your last name?");
        LastName  = keyboard.next();
        System.out.println("What is your grade (as a number)");
        Grade = keyboard.nextInt();
        System.out.println("What is your login name?");
        LoginName = keyboard.next();
        System.out.println("What is your Student ID number?");
        IDNum = keyboard.nextInt();
        System.out.println("And finally, what is your GPA? (Up to 4.0");
        GPA = keyboard.nextInt();

        System.out.println("Your information:");
        System.out.println("Login: " + LoginName);
        System.out.println("ID: " + IDNum);
        System.out.println("Name:" + LastName + ", " + FirstName);
        System.out.println("GPA: " + GPA);
        System.out.println("Grade: " + Grade);
    }
}
Scinerio
  • 35
  • 5

1 Answers1

1

There is no problem with not closing stdin, the JVM will take care of it for you and there is no resource leak.

Apparently you have some static analysis functionality built into the IDE that is looking for the scanner (likely anything that is Closeable triggers this) to get closed, not finding it, and generating a warning for you.

Scanner implements Closeable so it has a close method. Add a line to the end of the main method, like

keyboard.close()

Also add throws Exception to the main method signature, since close throws IOException. Again, not something that is actually going to happen with stdin. Scanner is throwing IOException because it handles all kinds of input streams, and there are cases where a network problem can disrupt things.

This should make your warning go away.

As for the stacktrace, what are you entering at the keyboard? Your scanner is calling nextInt so it is expecting an integer input. If you put a decimal point in there it won't be able to handle it. Consider changing your grade's type to BigDecimal.

Just because a number has a decimal point in it is not sufficient reason to use a floating point type. Floating-point is good for certain kinds of calculations. For cases when there is no calculation involved, and you don't want any difference between what you entered and what the program shows you, it's simpler to use BigDecimal.

Community
  • 1
  • 1