空指针异常密码程序

时间:2021-04-08 17:48:33

标签: java encryption hashmap

这是向我抛出此错误的代码:

我试图让输出文本显示加密的代码,但我对空指针的位置感到困惑 请帮忙!

线程“main”中的异常java.lang.NullPointerException

String clearText = sb.toString();
        String normalText=clearText.toLowerCase();
        System.out.println(normalText);
        System.out.println("Enter shift factor!");
        Scanner sc = new Scanner(System.in);
        int shiftFactor = sc.nextInt();

        //Create a HashMap
        //A hash map takes keys and values, which are both Characters in this case.
        HashMap<Character, Character> alphaMap = new HashMap<Character, Character>();
        int shift;
        //Get the text from the app and store it in a String variable.
        String textNum = String.valueOf(shiftFactor);
        //Check to see if a "Shift Factor" value was entered.
        //If there wasn't, set shift to zero,
        //Otherwise parse the input value to an integer so we can use it.
        if(!textNum.equals("")){
            shift = Integer.parseInt(textNum)%26;
        }
        else{
            shift = 0;
        }
        //Map every letter of the alphabet to another letter in the alphabet, shifted by x places.
        for(int i=0; i<alphabet.length(); i++){
            alphaMap.put(alphabet.charAt(i), alphabet.charAt((i+shift)%26));
        }
        //Get input text and put it all to lower-case so it's easy to convert
        String inputText = normalText.toLowerCase();
        String outputText = "";
        //Go to each letter and switch it with it's shifted counterpart
        for(int j=0; j<inputText.length(); j++){
            outputText = outputText.concat(alphaMap.get(inputText.charAt(j)).toString());
        }
        //Output the encrypted text
        System.out.println(outputText);
    }

0 个答案:

没有答案
相关问题