它告诉我,我有一个未捕获的例外,我错过了什么?

时间:2016-02-26 21:36:20

标签: java exception

程序应该接受一个代表您将输入多少个短语的数字,然后检查字母表中的每个字母是否都在所述短语中,如果没有,则按字母顺序打印丢失的字母。

import java.util.Arrays;
import java.util.Scanner;

public class Activity {

    public static void main(String[] args) {
        Activity test = new Activity();
        test.pangram();
    }


    public static void pangram() {
        Scanner sc = new Scanner(System.in);
        int numberPhrases = sc.nextInt();

        String[] inputs = new String[numberPhrases];

        for (int p = 0; p < numberPhrases; p++) {
            Scanner s = new Scanner(System.in);

            inputs[p] = s.nextLine().toLowerCase();
        }

        for (int i = 0; i < numberPhrases; i++) {
            String st = inputs[i];

            String pangram = "";
            String missing = "";

            if (!st.contains("a"))
                missing = missing + "a";
            if (!st.contains("b"))
                missing = missing + "b";
            if (!st.contains("c"))
                missing = missing + "c";
            if (!st.contains("d"))
                missing = missing + "d";
            if (!st.contains("e"))
                missing = missing + "e";
            if (!st.contains("f"))
                missing = missing + "f";
            if (!st.contains("g"))
                missing = missing + "g";
            if (!st.contains("h"))
                missing = missing + "h";
            if (!st.contains("i"))
                missing = missing + "i";
            if (!st.contains("j"))
                missing = missing + "j";
            if (!st.contains("k"))
                missing = missing + "k";
            if (!st.contains("l"))
                missing = missing + "l";
            if (!st.contains("m"))
                missing = missing + "m";
            if (!st.contains("n"))
                missing = missing + "n";
            if (!st.contains("o"))
                missing = missing + "o";
            if (!st.contains("p"))
                missing = missing + "p";
            if (!st.contains("q"))
                missing = missing + "q";
            if (!st.contains("r"))
                missing = missing + "r";
            if (!st.contains("s"))
                missing = missing + "s";
            if (!st.contains("t"))
                missing = missing + "t";
            if (!st.contains("u"))
                missing = missing + "u";
            if (!st.contains("v"))
                missing = missing + "v";
            if (!st.contains("w"))
                missing = missing + "w";
            if (!st.contains("x"))
                missing = missing + "x";
            if (!st.contains("y"))
                missing = missing + "y";
            if (!st.contains("z"))
                missing = missing + "z";
            if (missing.equals(""))
                System.out.println("pangram");
            else {
                missing.toLowerCase();
                char[] chars = missing.toCharArray();
                Arrays.sort(chars);
                String sorted = new String(chars);
                missing = sorted;
                System.out.println("missing " + missing);
            }
        }
    }
}

3 个答案:

答案 0 :(得分:0)

nextInt的{​​{1}}和nextLine方法都会抛出需要捕获的异常。

如果您使用的是体面的IDE,它应该告诉您。

答案 1 :(得分:0)

如果您输入的是字符串而不是数字,则numberarray4会抛出异常。像这样包装以避免该异常:

sc.nextInt()

答案 2 :(得分:0)

此代码工作正常但是当您输入与数字不同的字符时,它会导致java.util.InputMismatchException。如果这是您要处理的异常,只需使用try/catch(接受整数的扫描程序)来代码。

改变这个:

Scanner sc = new Scanner(System.in);
int numberPhrases = sc.nextInt();

类似于:

Scanner sc = new Scanner(System.in);
try{    
  int numberPhrases = sc.nextInt();
} catch (InputMismatchException e) {
  system.out.println("Wrong format");
}