-2

I know that the ArrayIndexOutOfBoundsException means that you're trying to access something not defined in the array, and tried to look up solutions. All the solutions say that you need to use < rather than =<, but that's about it. I don't understand why my loop below keeps giving me ArrayIndexOutOfBoundsException erros.

 for (int i=0; i < myMessage.length(); ){
                eInteger = eNumbers[i];
                myInteger = numbers[i];
                System.out.println(eInteger + " " + myInteger);
                character = myInteger - eInteger;
                stringCharacter = Integer.toString(character);
                //decryptedMessage = decryptedMessage + " " + stringCharacter;
                System.out.println(character);
                i++;
            }

I've tried int i=0, int i = 1, myMessage.length() - 1. It shouldn't be attempting to give me something further than the array, but I don't know.

Full code:

    public class Decrypt {
    private String myMessage;
    private String e = "2718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932003059921817413596629043572900334295260595630738132328627943490763233829880753195251019011573834187930702154089149934884167509244761460668082264800168477411853742345442437107539077744992069551702761838606261331384583000752044933826560297606737113200709328709127443747047230696977209310141692836819025515108657463772111252389784425056953696770785449969967946864454905987931636889230098793127736178215424999229576351482208269895193668033182528869398496465105820939239829488793320362509443117301238197068416140397019837679320683282376464804295311802328782509819455815301756717361332069811250996181881593041690351598888519345807273866738589422879228499892086805825749279610484198444363463244968487560233624827041978623209002160990235304369941849146314093431738143640546253152096183690888707016768396424378140592714563549061303107208510383750510115747704171898610687396965521267154688957035035402123407849819334321068170121005627880235193033224745015853904730419957777093503660416997329725088687696640355570716226844716256079882651787134195124665201030592123667719432527867539855894489697096409754591856956380236370162112047742722836489613422516445078182442352948636372141740238893441247963574370263755294448337";
    private String stringCharacter;
    private int character;
    private int myInteger;
    private int eInteger;
    private String decryptedMessage = "";

    public Decrypt(String myMessage){
        this.myMessage = myMessage;
    }

    public String Decryption(){

        String[] splitMessage = myMessage.split(" ");
        int[] numbers = Arrays.stream(splitMessage)
                .mapToInt(Integer::parseInt).toArray();

        String[] eMessage = e.split("");
        int[] eNumbers = Arrays.stream(eMessage)
                .mapToInt(Integer::parseInt).toArray();

        for (int i=0; i < myMessage.length(); ){
            eInteger = eNumbers[i];
            myInteger = numbers[i];
            System.out.println(eInteger + " " + myInteger);
            character = myInteger - eInteger;
            stringCharacter = Integer.toString(character);
            //decryptedMessage = decryptedMessage + " " + stringCharacter;
            System.out.println(character);
            i++;
        }
        return stringCharacter;
    }

    public String toString(){
        return "Message: " + Decryption();
    }

}

When I run my program (the extra code is in a driver) I get the following:

Welcome to this cryptographic program! Would you like to encrypt or decrypt a message?
Please enter 1 to encrypt, and 2 to decrypt.
2
Thank you! Please type the message you would like to encrypt or decrypt.
22 12 20 28
2 22
Exception in thread "main" 20
7 12
5
1 20
19
8 28
20
java.lang.ArrayIndexOutOfBoundsException: 4
    at Program4.Decrypt.Decryption(Decrypt.java:30)
    at Program4.Decrypt.toString(Decrypt.java:42)
    at java.lang.String.valueOf(Unknown Source)
    at java.io.PrintStream.println(Unknown Source)
    at Program4.Driver.main(Driver.java:28)
Sarah
  • 15
  • 6
  • 3
    You definitely don't want to get the -1 element of eNumber or number. – bhspencer Feb 09 '15 at 02:36
  • 1
    In addition to telling you that you've provided an index outside the range of the array, an `ArrayIndexOutOfBoundsException` also tells you *what* index you provided. (It's not obviously marked, but it's there. Just look for the number in the exception-message.) That can help you figure out which end of the array you're walking off the end of, which would help you recognize the issue that bhspencer points out in his/her comment. – ruakh Feb 09 '15 at 02:38
  • 1
    (And more generally: *every* stacktrace -- well, with rare exceptions -- will tell you what line triggered the exception. You absolutely must look at that, and if/when you post code here that triggers an exception, you should state which line you identified as its source.) – ruakh Feb 09 '15 at 02:40
  • You're testing the length of `myMessage`; but not of `eNumbers` or of `numbers`. Which line is throwing the Exception? – Elliott Frisch Feb 09 '15 at 02:45
  • What you need to do is have the array index be in bounds. Add println statements that capture the values that are relevant and work backwards to the point of failure. Forget about "rules" like using `<` instead of `<=`, since those only work in certain simple-minded cases. Instead, think through the logic. – Hot Licks Feb 09 '15 at 02:46
  • And identify line 30 in your listing above. – Hot Licks Feb 09 '15 at 02:47
  • myInteger = numbers[i]; This is line 30. – Sarah Feb 09 '15 at 02:50
  • Do this: Before the `for` loop print out `myMessage.length()` and `numbers.length`. Are they the same? If not, why not? And if they aren't the same, what does that do to your algorithm? – Hot Licks Feb 09 '15 at 02:54
  • Does this answer your question? [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Danubian Sailor Sep 27 '21 at 20:03

1 Answers1

0

The problem is that myMessage.length() is the number of characters in myMessage, whereas numbers.size is the number of integers represented in myMessage.

In your example run, myMessage is "22 12 20 28", which has 11 characters so you are iterating from 0 to 10; but numbers is an array of just four numbers (0 through 3), so numbers[i] will raise this exception for any i greater than 3.

If I'm understanding correctly what you are trying to do, you just need to change this:

        for (int i=0; i < myMessage.length(); ){

to this:

        for (int i=0; i < numbers.size; ){
ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Thank you so much! I would've had no idea. That worked, and now the answer makes complete sense. Thank you again! – Sarah Feb 09 '15 at 03:04