Malthe22
Malthe22

Reputation: 15

How can I encrypt numbers to a correct String

I'm trying to make a decryption method by inputting numbers corresponding to a certain letter.

With the current code, when I input 25 I just get "ab" outputted. Can someone point me in the correct direction?

I've tried using char ch = Character.toLowerCase(str.charAt(i)); and having ch in my switch, however this gives me a problem when I reach numbers 10+.

public void decryptNumbers() {
        String str, newString = " ";
        
        System.out.print("Numbers to decrypt: ");
        try {

            Scanner sc = new Scanner(System.in);
            str = sc.nextLine();

            for (int i = 0; i < str.length(); i++) {
                int test1 = Integer.parseInt(String.valueOf(i));


                switch (test1) {
                    case 0:
                        newString = newString + "a";
                        break;
                    case 1:
                        newString = newString + "b";
                        break;
                    case 2:
                        newString = newString + "c";
                        break;
                    case 3:
                        newString = newString + "d";
                        break;
                    case 4:
                        newString = newString + "e";
                        break;
                    case 5:
                        newString = newString + "f";
                        break;
                    case 6:
                        newString = newString + "g";
                        break;
                    case 7:
                        newString = newString + "h";
                        break;
                    case 8:
                        newString = newString + "i";
                        break;
                    case 9:
                        newString = newString + "j";
                        break;
                    case 10:
                        newString = newString + "k";
                        break;
                    case 11:
                        newString = newString + "l";
                        break;
                    case 12:
                        newString = newString + "m";
                        break;
                    case 13:
                        newString = newString + "n";
                        break;
                    case 14:
                        newString = newString + "o";
                        break;
                    case 15:
                        newString = newString + "p";
                        break;
                    case 16:
                        newString = newString + "q";
                        break;
                    case 17:
                        newString = newString + "r";
                        break;
                    case 18:
                        newString = newString + "s";
                        break;
                    case 19:
                        newString = newString + "t";
                        break;
                    case 20:
                        newString = newString + "u";
                        break;
                    case 21:
                        newString = newString + "v";
                        break;
                    case 22:
                        newString = newString + "w";
                        break;
                    case 23:
                        newString = newString + "x";
                        break;
                    case 24:
                        newString = newString + "y";
                        break;
                    case 25:
                        newString = newString + "z";
                        break;
                    case 26:
                        newString = newString + "æ";
                        break;
                    case 27:
                        newString = newString + "ø";
                        break;
                    case 28:
                        newString = newString + "å";
                        break;

                }
            }
        }
        catch(Exception e)
            {
                e.printStackTrace();
            }
            System.out.println("Decryption done:" + newString);
        }

Upvotes: 0

Views: 133

Answers (1)

Thomas Kl&#228;ger
Thomas Kl&#228;ger

Reputation: 21630

I think I begin to understand your previous code (it would still be better to include it in the question):

You had

char ch = Character.toLowerCase(str.charAt(i)); 
switch (ch) { 
    case '0': 
        newString = newString + "a"; 
        break;
    case '1':
        newString = newString + "a"; 
        break;
    // more cases here
}

and you don't know how to handle the case for the letter "k", because for that you would need a case '10':.

This is a problem, because there is no character for "10" - the number "10" consists of two characters. You can't handle that the with code that only looks at a single character at a time.

This hints at a deeper problem with your design. In your design the input contains "10" it can mean two different things:

  • it could mean "ba"
  • it could mean "k"

With the current design you can't distinguish these two cases.

One way to fix the design would be to separate the numbers for encoded letters, for example with a space (' ') character. That would mean that

  • "ba" is encoded as "1 0"
  • "k" is encoded as "10"

which would allow you to distinguish the two encodings.

Upvotes: 0

Related Questions