简体   繁体   中英

Java inputMismatchException in calculator

i'm making a calculator but i'm having trouble using the scanner input, i declared the variables as double but if i use i get this error:

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextDouble(Unknown Source)
    at ProgramTUI.main(ProgramTUI.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)

Here's the code:

import java.util.Scanner;
    public class ProgramTUI {

    public static double add(double a, double b) {

      return a+b;
    }

    public static double substract(double a, double b) {

      return a-b;
    }    

    public static double multiply(double a, double b) {

      return a*b;
    }

    public static double divide(double a, double b) {

      return a/b;
    }    

    public static void main(String[] args) {

        int choice;
        double a, b;

        System.out.println("1- Add");
        System.out.println("2- Sub");
        System.out.println("3- Mul");
        System.out.println("4- Div");
        System.out.print("Elige la operacion -");
        Scanner var = new Scanner(System.in);

        choice = var.nextInt();
        System.out.print("Introduce el primer numero -");
        a = var.nextDouble();
        System.out.print("Introduce el segundo numero -");
        b = var.nextDouble();

        switch (choice) {
            case 1:
                System.out.print(add(a,b));
                break;

            case 2:
                System.out.print(substract(a,b));
                break;

            case 3:
                System.out.print(multiply(a,b));
                break;

            case 4:
                System.out.print(divide(a,b));
                break;
        }
    }
}

With int numbers the program work just fine, i declared a and b with var.nextDouble(); so i don't know what's going on.

我相信您使用的是错误的小数点分隔符,请尝试如下操作:

Scanner var = new Scanner(System.in).useLocale(Locale.ENGLISH); 

You program will fail if whatever you input can not be interpreted as int for var.nextInt(); and double for var.nextDouble();

This might be due to the system locale having another number format.

You can use this to force the English format:

Scanner var = new Scanner(System.in).useLocale(Locale.ENGLISH); 

Or you can get the Numbers as strings and parse them

      try 
      {  
         value = Integer.parseInt(value);  
      } 
      catch (NumberFormatException e) 
      {  
         conversionfailed = true;  
      }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM