2017-04-16 2 views
-1

Wie kann ich die Ausgabe dieses Programms anzeigen? Wenn ich es starte, wird es einen InputMismatchException werfen."Ausnahme im Thread" main "java.util.InputMismatchException"

Hier ist der gesamte Code:

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

Sie können die Ausgabe erst anzeigen, wenn Sie Ihr Programm ausführen lassen. Auf welche Zeile wird die Ausnahme geworfen? –

+0

Das Programm läuft, aber dies zeigt die Fehlerzeile 93 "int N = in.nextInt();" Wenn ich es die Eingabe eingeben, wird es zeigen Ausnahme im Thread "Haupt" java.util.InputMismatchException " bei Java .util.Scanner.throwFor (Unbekannte Quelle) bei java.util.Scanner.next (Unbekannte Quelle) bei java.util.Scanner.nextInt (Unbekannte Quelle) bei java.util.Scanner.nextInt (Unbekannte Quelle) bei Try.main (Try.java:93) –

+1

@DaNiel Was genau ist die Eingabe, die Sie eingeben? Wenn es nicht eine "Integer" ist, dann wird es eine Ausnahme werfen ... – kunruh

Antwort

0

den gesamten Code in einem Try-Block beilegen und den Stack-Trace in dem catch-Block drucken und sieht, auf der Linie dort Fehler sind und entsprechend handeln.

+0

Wie kann ich das tun Sir? –

+0

Jetzt ist es nicht notwendig, wie Sie bereits diesen Fehler in Zeile 93 gesagt haben. Ich denke, Sie geben keine ganze Zahl ein und erhalten daher diesen Fehler. –

+0

Okay Sir Danke für die Hilfe :) –

Verwandte Themen