Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

My program is supposed to gets a phrase from the user then returns to the user an encrypted code of their choice (either ROT13 or ATBASH) of the phrase they entered. My code compiles and everything and lets the user input the required stuff but when they enter the phrase to be encrypted, nothing happens.. like the new encrypted code doesnt show up, and i dont know what wrong with it! Please help! Thank you!

   import java.io.*;

public class J4_1_EncryptionVer4
{
  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));//BufferedReader reads user input

    //String array letterA[] is initialized
    String [][] letterA = new String [][]{
    {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"},
    {"N","O","P","Q","R","S","T","U","V","W","X","Y","Z","A","B","C","D","E","F","G","H","I","J","K","L","M"},
    {"Z","Y","X","W","V","U","T","S","R","Q","P","O","N","M","L","K","J","I","H","G","F","E","D","C","B","A"},
    };

    System.out.println ("Enter '1' for ROT13 or '2' for ATBASH");//asks user to choose method
    String numA = myInput.readLine();//reads user input and assigns it to string
    int num = Integer.parseInt (numA);//converts string to integer
    int a = 0;//int a is declared

    if (num == 1){//if user enters 1
          a = 1;//set a to 1
    }
    if (num == 2) {//end if//if user enters 2
          a = 2;//set a to 2
        }//end if
    System.out.println ( a);
    System.out.println(num);

    System.out.println ("Please enter a phrase: ");//asks user to enter phrase
    String message = myInput.readLine();//reads user input and assigns it to string

    int x = 0; //declares int var x

    System.out.println ("Your Encrypted code is: ");//prints out scentence


    while (x < message.length())//while loop will run while x is less that the phrase length          
    {
      String text = message.toUpperCase();//converts user input to upper case
      String letter = Character.toString(text.charAt(x));//extracts character from string and assigns it to another string letter

      x++;//increments x by 1 each time

      for(int i=0; i<letterA.length; i++)//for loop declares int i = 0, will run while i is less than the the length of the array letterA, and i will increment by 1 each time
      {
        if(letter.equals(letterA[a][i]))//if the letter is equal to letterA[i]
        {
          System.out.print (letterA[a][i]);//print out the corresponding letter

          break;//breaks from loop

        }//end if

        else if (letter.equals(" "))//else id the letter is equal to a space
        {
          System.out.print(" ");//prints out space
          break;//breaks from loop
        }//end else if        
      }//end for loop
    }//end while loop
  }//end main
}//end class
share|improve this question
2  
I strongly recommend learning to use a debugger. – David Wallace 18 hours ago

2 Answers

up vote 2 down vote accepted

This doesn't work because letterA.length is 3, so your for loop only runs through 3 iterations, instead of 26.

share|improve this answer
As opposed to letterA[0].length, letterA[1].length and letterA[2].length all of which are 26 – Richard Tingle 18 hours ago
hi! thanks for the answer... but im kind of confused of by what you mean – Maddie Cal 17 hours ago
@Maddie LetterA is a 2 dimensional array, you've got A-Z in letterA[0] N-M in letterA[1] and Z-A in letterA[2]. So the length of letterA is 3 but in your code I think you expect it to be 26, which is the length of the second dimension arrays – Richard Tingle 17 hours ago
Your for loop will iterate 3 times - once with i = 0, once with i = 1 and once with i = 2. So depending on the value of a, you'll only compare letter against A, B and C; or against N, O and P; or against Z, Y and X. Nothing will happen for other letters. – David Wallace 17 hours ago
@DavidWallace so is my goal to get the loop to run for the length of the phrase that is entered message.length()? – Maddie Cal 17 hours ago
show 2 more comments

I think you should change your for loop to

    for (int i= 0; i < letterA[0].length ; i++ ) {
           if (letter.equals(letterA[0][i]) {
               System.out.print(letterA[a][i]);
               break;              
           }
           else {
                // .........,.......
           }
    }

First use the first array as basis. Compare the letters then if they are equal then get the index of that letter to be used in encrypting

I'm using a bloody phone ryt now so I didn't really compile your code

share|improve this answer
Oh my what happened to my code – misserandety 17 hours ago
Thnks for the edit – misserandety 17 hours ago
Thanks for the help – Maddie Cal 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.