使用Scanner进行JAVA InputMismatchException

时间:2017-12-23 01:24:56

标签: java exception input

我希望使用以下语法对Scanner进行一些输入:

1 2  --> Number1 Number2 --> First Number
+    --> Operator
2 4  --> Number1 Number2 --> Second Number

如果我进入。 (DOT)程序应该中止,并且应该执行Rational Class的一些数学方法。这是另一个例子:

1 2   --> Number1 Number2 --> First Number
+     --> Operator
2 4   --> Number1 Number2 --> Second Number
-     --> Operator
4 -3  --> Number1 Number2 --> Third Number

这是我的实际代码:

    Scanner sf = new Scanner(System.in);
    Rational[] number = null;
    int number_one, number_two = 0;
    String operator = "";

    while(sf.hasNextInt()){

        if(sf.next().equals(".")){ 
            for(int i = 0; i < number.length; i++){
                switch (operator){
                    case "+":  System.out.println(number[i].add(number[i+1]).toString());
                               break;
                    case "-":  System.out.println(number[i].sup(number[i+1]).toString());
                               break;       
                    case "*":  System.out.println(number[i].mul(number[i+1]).toString());
                               break;
                    case "/":  System.out.println(number[i].div(number[i+1]).toString());
                               break;   
                    default:    System.out.println("You can only use +|-|*|/");
                    }//Switch
                }//For 
        }
        else{

            number_one = sf.nextInt();
            number_two = sf.nextInt();
            operator = sf.next();

            number = new Rational[]{new Rational(number_one , number_two )};    
        }

    }//While

但是我收到以下错误消息:

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 RationalTest.main(RationalTest.java:46) --> number_two = sf.nextInt();

修改

我现在得到了它,但我仍然有问题。如果我进入(DOT)程序应该中止。但是在我输入一个点后,我需要输入任何内容,之后它将被取消:

1 2
+
3 4
-
2 1
.   --> it should be canceled here
1   --> But I have to enter something else. After that it will be canceled

新守则:

public static void main(String[] args) {

    Scanner sf = new Scanner(System.in);
    int zaehler = 0; 
    int nenner = 0;
    String operator = "";

    while(sf.hasNextInt()){

        String z = Integer.toString(zaehler);
        String n = Integer.toString(nenner);

        if(z.equals(".") || n.equals(".") || operator.equals(".")){ 
            break;
        }else{

            int zI = Integer.parseInt(z);
            int nI = Integer.parseInt(n);

            zI = sf.nextInt();
            nI = sf.nextInt();
            operator = sf.next();
        }

    }

    sf.close();
}

1 个答案:

答案 0 :(得分:0)

错误是当你打电话给&#34; number_two = sf.nextInt();&#34;这里有String输入。这就是为什么: 1.-扫描仪的第一个要素:

if(sf.next().equals(".")) //An int.

2.-扫描仪的第二个要素:

number_one = sf.nextInt(); //An int

3.-扫描仪的第三个要素:

number_two = sf.nextInt(); //An String

你必须纠正这个。