-5

I am a beginner in java programming. I have a question on whats going on, whenever i try to compile it, it keeps giving me error like this :

Exception in thread "main" java.util.InputMissMatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextDouble(Scanner.java:2413)
at MoreUserInputOfData.main(MoreUserInputOfData.java:28)

if someone would like to help me clean up my code as well, it wouldn't hurt..

import java.util.Scanner;
public class MoreUserInputOfData{
    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

String firstName, lastName, loginName;
int grade, studentID;
double gpa;

System.out.println("Please enter the following information, so i cann sell it for a profit!");

System.out.print("First name: ");
firstName = keyboard.next();

System.out.print("Last name: ");
lastName = keyboard.next();

System.out.print("Grade (9-12): ");
grade = keyboard.nextInt();

System.out.print("Student ID: ");
studentID = keyboard.nextInt();

System.out.print("Login: ");
loginName = keyboard.next();

System.out.print("GPA (0.0-4.0): ");
gpa = keyboard.nextDouble();

System.out.println("");
System.out.println("Your information:");
System.out.println("Login:"+loginName);
System.out.println("ID: "+studentID);
System.out.println("Name: "+lastName+", "+firstName);
System.out.println("GPA: "+gpa);
System.out.println("Grade: "+grade);
}

}

  • Please post a [mcve], i.e. remove all code that's not required to reproduce the issue – Thomas Weller Aug 19 '17 at 23:34
  • Maybe line 28 is `keyboard.nextDouble();` and your local settings require something else than a dot? What did you enter? – Thomas Weller Aug 19 '17 at 23:35
  • Related: https://stackoverflow.com/questions/20372814/java-util-inputmismatchexception-by-reading-a-double – Thomas Weller Aug 19 '17 at 23:37
  • You may find this link helpful https://stackoverflow.com/questions/17150627/scanner-double-value-inputmismatchexception – Perry Aug 19 '17 at 23:39
  • Error simply means that the value entered for GPA is not a valid `double` number. Which part of that is confusing? If you think it should work, then show the value you're entering, so we can help identify the problem. And remember, as Thomas Weller said, `nextDouble()` uses your locale, so if you location uses `,` as decimal separator, you have to enter `0,5`, not `0.5`. – Andreas Aug 19 '17 at 23:40

1 Answers1

0

Look at your code at line 28. When you input data, maybe you input the wrong type that the variable can't accept. For example: nextDouble require dot like 0.3 but you input 3.

For nextDouble : InputMismatchException -- if the next token does not match the Float regular expression, or is out of range

you should write it by prevent exception like:

 // find the next double token and print it
   // loop for the whole scanner
   while (keyboard.hasNext()) {
   ...
   // if the next is a double, print found and the double
   System.out.print("GPA (0.0-4.0): ");
   if (keyboard.hasNextDouble()) {
      gpa = keyboard.nextDouble();
   } else {
      System.out.print("Not double");
   }
   ...
   }
rensothearin
  • 670
  • 1
  • 5
  • 24
  • Thanks for your suggestion, Just the opposite, when I enter the number eg 3.70 it will appear the error message as before, but if I enter the number eg 3, it will not appear error message and on the line part gpa, the result is 3.0 – Wahyu Indra Kusuma Aug 20 '17 at 00:06