Brandon Latimer
Brandon Latimer

Reputation: 9

Creating a code deciphering program

I have posted about this before, but I am still having some problems. I have to decode a message given by the user.

The message consists of a series of numbers and should be decoded using these substitutions:

1 = D
2 = W
3 = E
4 = L
5 = H
6 = O 
7 = R

For instance, 5344626741 is HELLOWORLD.

I have tried many things, and gotten close, but the output is not correct. I think there is a problem searching the string for certain characters.

Any help is greatly appreciated, here is my code:

public static void main(String[] args){
    Scanner in = new Scanner(System.in);

    System.out.println("Please enter 10 numbers, after each number you put in, press enter. The numbers can only be from 1 - 7.");

    int numInputs = 0;
    String code = "", deciphered = "";
    int input = 0, charNumber = 7;
    //5344626741
    do{
        System.out.println("Please enter a number: ");
        input = in.nextInt();
        if(input>=8 || input<=0){
            System.out.println("Please enter a different number: ");
            input = in.nextInt();
            code+=input;
            numInputs++;
        }
        else{
        code+=input;
        numInputs++;
        }
    }while(numInputs < 10);

    System.out.println("Your code is " + code);
    for(int add = 0; add < 10; add++){
        switch(code.charAt(charNumber)){
        case '1': deciphered+="D";
        case '2': deciphered+="W";
        case '3': deciphered+="E";
        case '4': deciphered+="L";
        case '5': deciphered+="H";
        case '6': deciphered+="O";
        case '7': deciphered+="R";
        break;
        default: System.out.println("Something went wrong! Try again with numbers only 1 - 7.");
        charNumber++;
        }
    }

    System.out.println("The output is: "+deciphered);
    in.close();
}

Upvotes: 0

Views: 440

Answers (2)

Rooniesax
Rooniesax

Reputation: 1

    Scanner in = new Scanner(System.in);
    int numInputs = 0;
    int input = 0;
    String sum = "";
    String deciphered = "";
    
    while(numInputs < 10)
    {
        int x = numInputs + 1;
        System.out.println("Enter the " + x + "º number:");
        input = in.nextInt();
        if (input >= 1 && input <= 7)
        {    
            sum += input + " ";
            numInputs++;
        } else
        {
            System.out.println("Remember the numbers range is 1 to 7, please try again.");
        }
    }
    System.out.println("The encrypted code of the following numbers " + sum + "is:");
    
    // For test the exercise try with this number 5 3 4 4 6 2 6 7 4 1
    
    for (int i = 0; i < sum.length(); i++)
    {
        char a = sum.charAt(i);
        switch (a)
        {
            case '1': 
                deciphered += "D";
                break;
            case '2': 
                deciphered += "W";
                break;
            case '3': 
                deciphered += "E";
                break;
            case '4': 
                deciphered += "L";
                break;
            case '5': 
                deciphered += "H";
                break;
            case '6': 
                deciphered += "O";
                break;
            case '7': 
                deciphered += "R";
                break;
            case ' ':
                deciphered += " ";
                break;
         }
    }
    System.out.println(deciphered);

Upvotes: 0

Michael J. Gray
Michael J. Gray

Reputation: 9906

This looks like a simple typographic error and one logic error. I believe switch(code.charAt(charNumber)) should be changed to switch(code.charAt(add)) and at the bottom of the second for loop you should remove charNumber++;

You're also missing break statements after each case statement.

Basically, in your last for loop you need to make sure you are iterating with the right indexer. Here, it looks like you've initialized charNumber to 7 at the beginning of your program and thus you're seeing an error when you exceed the string length.

Upvotes: 1

Related Questions