Question

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]);
      }

    }
  }
}
}
Was it helpful?

Solution 2

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

OTHER TIPS

Three problems:

  1. while (x <= message.length()) should be while (x < message.length())
  2. x++ should be move after text.charAt(x)
  3. use equals to compare string value.

and of course you can only use A-Z to test

For starters the reason for the exception is array index out of bounds line 28. You're not bothering to check that i is within bounds. Also, you're taking a letter from the user entered message, which can be anything from the keyboard, then looking for that in letterA which is only the 26 upper case alphas! Basically you walk off the end of letterA never finding letter.

You are providing letterA [] for Capital letter characters only. Your comparing loop is some what ok. But you have to check your while (letter != letterA[i]) loop.

Be sure while giving input to program as capital letter character only to test the ouput.

import java.io.*;

public class JavaApplication1 {

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

        char 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'};
        char 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();
        message = message.toUpperCase();
        char letterC[] = new char[message.length()]; 

        int x = 0;
        for (int j = 0; j < message.length(); j++) {
            for (int i = 0; i < letterA.length; i++){
                if(message.charAt(j)==letterA[i]){
                    letterC[j]=letterB[i];
                }
            }

        }

        for (int j = 0; j < message.length(); j++) {
            System.out.print(letterC[j]);
        }
        System.out.println("  -- done");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top