Search code examples
javaarraysstringrot13

String Arrays in Java Program


I have to make a program which gets a phrase from the user then returns to the user an encrypted code of the phrase they entered. We were to use arrays or enumerated lists to hold the data. Im using ROT13 to encode (replaces each English letter with the one 13 places forward or back along the alphabet). My program runs but it only allows me to input a phrase, after that is says :

java.lang.ArrayIndexOutOfBoundsException: 26
    at J4_1_EncryptionVer2.main(J4_1_EncryptionVer2.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Im not sure whats wrong with my program! Please help! Thanks

        import java.io.*;

public class J4_1_EncryptionVer2
{
  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));

   String letterA [] = {"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"};
   String letterB [] = {"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"};

    System.out.println ("Please enter a phrase: ");
    String message = myInput.readLine();

    int x = 0; 
    while (x < message.length()){

      String text = message;
      String letter = Character.toString(text.charAt(x));
        x++;

      int i = 0;

     if(letter.equals (letterA[i])){
        System.out.println (letterB[i]);
      }
      while (!(letter.equals (letterA[i]))){
        i++;
        if(letter.equals (letterA[i])){
        System.out.println (letterB[i]);
      }

    }
  }
}
}

Solution

  • Like other users have suggested above, you did not take care of array size .. and there is nothing stopping your while loop:

    while (!(letter.equals (letterA[i])))
    

    I am not much fan of while when dealing with arrays. In my opinion, this is how your outer while should look like.

          while (x < message.length())          
                {
                    String text = message;
                    String letter = Character.toString(text.charAt(x));
                    x++;
    
                    for(int i=0; i<letterA.length; i++)
                    {
                        if(letter.equals(letterA[i]))if(letter.equals(letterA[i]))
                        {
                            System.out.print (letterB[i]);
                            break;
                        }
                        else if (letter.equals(" "))
                        {
                            System.out.print(" ");
                            break;
                        }
                    }
                }
    

    I tested it on my machine and it works just fine.

    Input: THIS IS MY PLAINTEXT

    Output: GUVF VF ZL CYNVAGRKG