0

java -cp /tmp/NmkWULoGu6 Main Enter student name: mohammed

Enter

grade1: 2

grade2: 5

grade3: 4

Exception in thread "main" java.util.InputMismatchExceptionat 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:8)

The error is above I have been trying to fix the code for an hour and couldn't fix it is it wrong ?

The Code is below

import java.util.Scanner;

public class Main{

    public static void main(String args[]){

        Scanner scan = new Scanner(System.in);

        System.out.print("Enter student name: ");

        String name = scan.next();

        System.out.print("Enter \ngrade1: \ngrade2: \ngrade3: ");

        double grade1 = scan.nextDouble();

        double grade2 = scan.nextDouble();

        double grade3 = scan.nextDouble();

                double average = ((grade1 * 0.25) + (grade2 * 0.25) + (grade3 * 0.50));

        System.out.println(name + ", Your average is " + average);

        }
   } 
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Your code works fine other than the fact that `grade1: grade2: grade3:` all print at the same time, you want to have 3 different print statements between each `nextDouble` in order to format it correctly. – Nexevis Jan 04 '23 at 19:36
  • Does this answer your question? [nextDouble() throws an InputMismatchException when I enter a double](https://stackoverflow.com/questions/5929120/nextdouble-throws-an-inputmismatchexception-when-i-enter-a-double) – racraman Jan 04 '23 at 19:45
  • yeah i figured when i did split them to each of their own double the code worked – Braumasta Jan 04 '23 at 19:46

0 Answers0