“线程中的异常”主“java.util.InputMismatchException”

时间:2017-04-16 06:48:29

标签: java

如何显示此计划的输出?当我运行它时,它将抛出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 :(得分:0)

将整个代码包含在try块中,并在catch块中打印堆栈跟踪,并查看哪一行存在错误并采取相应措施。