2017-04-16 122 views
-1

我怎樣才能顯示這個程序的輸出?當我運行它時,它會拋出一個InputMismatchException’在線程異常」

這裏是整個代碼:

import java.util.HashMap; 
import java.util.Scanner; 

public class Try { 

    static HashMap< String, String> codeMap = new HashMap< String, String>(); 
    static HashMap< String, String> ref = new HashMap< String, String>(); 

    static void initMap() { 
     codeMap.put("A", ".-"); 
     codeMap.put("B", "-..."); 
     codeMap.put("C", "-.-."); 
     codeMap.put("D", "-.."); 
     codeMap.put("E", "."); 
     codeMap.put("F", "..-."); 
     codeMap.put("G", "--."); 
     codeMap.put("H", "...."); 
     codeMap.put("I", ".."); 
     codeMap.put("J", ".---"); 
     codeMap.put("K", "-.-"); 
     codeMap.put("L", ".-.."); 
     codeMap.put("M", "--"); 
     codeMap.put("N", "-."); 
     codeMap.put("O", "---"); 
     codeMap.put("P", ".--."); 
     codeMap.put("Q", "--.-"); 
     codeMap.put("R", ".-."); 
     codeMap.put("S", "..."); 
     codeMap.put("T", "-"); 
     codeMap.put("U", "..-"); 
     codeMap.put("V", "...-"); 
     codeMap.put("W", ".--"); 
     codeMap.put("X", "-..-"); 
     codeMap.put("Y", "-.--"); 
     codeMap.put("Z", "--.."); 
     codeMap.put("_", "..--"); 
     codeMap.put(".", "---."); 
     codeMap.put(",", ".-.-"); 
     codeMap.put("?", "----"); 

     ref.put(".-", "A"); 
     ref.put("-...", "B"); 
     ref.put("-.-.", "C"); 
     ref.put("-..", "D"); 
     ref.put(".", "E"); 
     ref.put("..-.", "F"); 
     ref.put("--.", "G"); 
     ref.put("....", "H"); 
     ref.put("..", "I"); 
     ref.put(".---", "J"); 
     ref.put("-.-", "K"); 
     ref.put(".-..", "L"); 
     ref.put("--", "M"); 
     ref.put("-.", "N"); 
     ref.put("---", "O"); 
     ref.put(".--.", "P"); 
     ref.put("--.-", "Q"); 
     ref.put(".-.", "R"); 
     ref.put("...", "S"); 
     ref.put("-", "T"); 
     ref.put("..-", "U"); 
     ref.put("...-", "V"); 
     ref.put(".--", "W"); 
     ref.put("-..-", "X"); 
     ref.put("-.--", "Y"); 
     ref.put("--..", "Z"); 
     ref.put("..--", "_"); 
     ref.put("---.", "."); 
     ref.put(".-.-", ","); 
     ref.put("----", "?"); 
    } 

    public static void main(String[] args) { 
     { 
      Scanner in = new Scanner(System.in); 
      System.out.println("Enter Code: "); 
      initMap(); 
      int N = in.nextInt(); 
      for (int i = 1; i <= N; i++) { 
       StringBuffer encry = new StringBuffer(in.next()); 
       StringBuffer inter = new StringBuffer(); 
       StringBuffer num = new StringBuffer(); 

       StringBuffer org = new StringBuffer(); 
       String tmp = new String(encry); 
       for(int j = 0; j < tmp.length(); j++) { 
        inter.append(codeMap.get(tmp.substring(j, j+1))); 
        num.append(codeMap.get(tmp.substring(j, j+1)).length());  
       } 
       num = num.reverse(); 
       int index = 0; 
       for(int j = 0; j < num.length(); j++) { 
        int t1 = Integer.valueOf(num.substring(j, j+1)); 
        StringBuffer con = new StringBuffer(); 
        for(int k = index; k < index+t1;k++) { 
         con.append(inter.substring(k, k+1)); 
        } 
        org.append(ref.get(new String(con))); 
        index += t1; 
       } 
       System.out.printf("%d: %s\n",i,org); 
      } 
     } 
    } 
} 
+1

,直到你得到你要運行的程序不能顯示輸出。這個異常會引發什麼? –

+0

程序運行,但這種示出了誤差線93「INT N = in.nextInt();」的時候我進入它的輸入端,然後它會顯示在java的螺紋 異常「主」 java.util.InputMismatchException」 .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) 在Try.main(Try.java:93) –

+1

@DaNiel到底是什麼,你正在進入輸入?如果它不是一個'Integer',那麼它會拋出一個異常... – kunruh

回答

0

括在try塊整個代碼,並打印在catch塊堆棧跟蹤,看到哪一行有錯誤,並採取相應的行動。

+0

我該怎麼做先生? –

+0

現在沒有必要,因爲你已經說過93行中的錯誤。我認爲你沒有輸入整數,因此得到這個錯誤。 –

+0

好的,先生,謝謝你的幫助:) –