簡體   English   中英

使用掃描儀的JAVA InputMismatchException

[英]JAVA InputMismatchException by using Scanner

我想要使​​用以下語法與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();
}

錯誤是當您調用“ number_two = sf.nextInt();”時 在這里,您有String輸入。 原因如下:1.- Scanner的第一個元素:

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

2.-掃描儀的第二個元素:

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

3.-掃描儀的第三個元素:

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

您必須對此進行更正。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM