I have the below Java code. When I enter an integer value in the input, it works ok (For ex -4), but when I enter 4.0, it breaks. The error is below. Can someone please help understand what is it that I am doing wrong? Thank you in advance.
import java.util.Scanner;
public class Main {
public static void main(String[] args ) {
int studentAge = 25;
double studentGPA = 3.45;
boolean hasPerfectAttendance = true;
String studentName = "Ivan Grey";
char studentFirstInitial = studentName.charAt(0);
char studentLastInitial = 'G';
System.out.println(studentAge);
System.out.println(studentGPA);
System.out.println(studentFirstInitial);
System.out.println(studentLastInitial);
System.out.println(hasPerfectAttendance);
System.out.println(studentName);
System.out.println(studentFirstInitial + " " + studentLastInitial +
" has a GPA of " + studentGPA);
System.out.println("What do you want to update it to?");
Scanner input = new Scanner(System.in);
studentGPA = input.nextDouble();
input.close();
System.out.println(studentFirstInitial + " " + studentLastInitial +
" now has a GPA of " + studentGPA);
}
}
Error is Exception in thread "main" java.util.InputMismatchException at java.base/java.util.Scanner.throwFor(Scanner.java:939) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextDouble(Scanner.java:2564) at Main.main(Main.java:24)