ROT13/ captain crunch continued

350 views Asked by At

The Captain Crunch decoder ring works by taking each letter in a string and adding 13 to it. For example, 'a' becomes 'n' and 'b' becomes 'o'. The letters "wrap around" at the end, so 'z' becomes 'm'.

this is what i've gotten after editing it a bit from peoples comments, but now it keeps telling me that output may have not been initialized and i have no clue why... also is there anything else i need to fix in my program?

in this case, i am only concerned with encoding lowercase characters

public class captainCrunch {
    public static void main (String[] Args) {

        Scanner sc = new Scanner(System.in);
        String input;

        System.out.print("getting input");
        System.out.println("please enter word: ");
        input = sc.next();

        System.out.print(" ");
        System.out.print("posting output");
        System.out.print("encoding" + input + " results in: " + encode(input));
    }//end of main

/*
*
*/

    public static String encode(String input){
        System.out.print(input.length());
        int length = input.length();
        int index;
        String output;
        char c;
        String temp = " ";

        for (index = 0; index < length; index++) {
            c = input.charAt(index);
            if(c >= 'a' && c <= 'm'){
                c += 13;
            }else if(c >= 'n' && c <= 'z'){
                c -= 13;
            }

            output = temp + (char)(c);
        }
        return output;
    }
}
2

There are 2 answers

2
Tim B On

It is because you have no value of output if the length of the for loop is 0. So if length == 0 then output will never have been set when you try and return it.

Fix it by doing:

String output="";

Additionally you you are only setting output to one string, you need to append to it each time around the loop, not set it to an entirely new value:

output += String.valueOf((char)(c));

Really you would be better off using a StringBuilder for this though.

StringBuilder output = new StringBuilder();

    for (index = 0; index < length; index++) {

        c = input.charAt(index);
        if       (c >= 'a' && c <= 'm') c += 13;
        else if     (c >= 'n' && c <= 'z') c -= 13;
        output.append((char)(c));
    }
    return output.toString();

For this sort of thing StringBuilder is a LOT more efficient than a String (since String are immutable new ones have to be made for every concatenation).

0
initramfs On

In your encode method the only assignment to output is within a for loop. Since a compiler cannot 100% confirm that the for loop will run (due to the usage of variables in the condition) there is a possibility that variable output gets returned uninitialized which is prohibited by the compiler.

Logically this would happen when a empty string is passed into the encode() method (since length = 0 and 0 is not less than 0 [in the for loop's condition]).

To solve this, assign a initial value to output such as null or a empty string as such:

String output = null;

or

String output = "";

The solution you choose will depend on what you intend to return if the method receives an empty string.