1

I am coding in a basic pin and user id system but I can't seem to figure out this indexing issue. I understand that arrays are indexed at 0 and in my code I am going through in a while loop (I tried a for loop but got the same issue) checking the i'th position of the array with the entered pin. for whatever reason I am getting this error:

Process: com.example.gabeskillerpcjr.assemblylineapp, PID: 17838 java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
    at com.example.gabeskillerpcjr.assemblylineapp.FourDtForm.exportVarifaction(FourDtForm.java:350)
    at com.example.gabeskillerpcjr.assemblylineapp.FourDtForm$3.onClick(FourDtForm.java:264)

Here is my simple while loop, int i is initialized at 0:

public void exportVarifaction(){
    String[] PinsNums = new String[]{"1415","1678","1923"};
    String[] PinNames = new String[]{"admin","test","test1"};
    String tmp;
    boolean done = false;
    while (!done) {
        tmp = PinsNums[i];
        if (tmp.equals(keyPadNumsEntered)) {
            result = PinNames[i];
            loggedOn = true;
            done = true;
        } else {
            ++i;
            result = "no logon";
        }
    }
}
2
  • 1
    you need to set done to true if i >= PinsNums.length; If tmp never equals keyPadNumsEntered, it will increment and never be done. Commented Jul 3, 2018 at 19:19
  • also, where is i set to zero? It should right before the loop starts, otherwise it might start at the wrong number depending on how many times this executes. Commented Jul 3, 2018 at 19:23

5 Answers 5

1

You're missing a condition to stop the loop after your iterated over the entire array. E.g.:

while (!done) {
    tmp = PinsNums[i];
    if (tmp.equals(keyPadNumsEntered)) {
        result = PinNames[i];
        loggedOn = true;
        done = true;
    } else {
        ++i;
        result = "no logon";
    }

    if (i == PinsNums.length) {
        done = true;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The while loop you are using and the done boolean are both completely unnecessary. You are getting this exception because what if 'tmp' never equals 'keyPadNumsEntered'? It will continue to iterate causing an IndexOutOfBoundsException This would be much more readable as a for loop and it should prevent the exception:

public void exportVarifaction(){
    String[] PinsNums = new String[]{"1415","1678","1923"};
    String[] PinNames = new String[]{"admin","test","test1"};
    String tmp;
    for (int i = 0; i < PinsNums.length; i++) {
        tmp = PinsNums[i];
        if (tmp.equals(keyPadNumsEntered)) {
            result = PinNames[i];
            loggedOn = true;
            break;
        } 
    }
    if (!loggedOn) {
        result = "no logon";
    }
}

1 Comment

There needs to be a second condition to break out of the loop when tmp is equal to keyPadNumsEntered. Otherwise unless i is equal to 2, result will always end up as "no logon".
0

You need to check that if loop on the array is complete by checking with variable i and length of the array.

 while (!done && i<PinsNums.length) {
        tmp = PinsNums[i];
        if (tmp.equals(keyPadNumsEntered)) {
            result = PinNames[i];
            loggedOn = true;
            done = true;
        } else {
            ++i;
            result = "no logon";
        }
    }

Comments

0

Your while loop does not terminate if it doesn't receive a pin that is in your array.

Just trace what happens if tmp = "9999"? and i = 2? your while loop will iterate a third time causing index out of bounds. I would rather go with a for loop:

for(int i = 0; i < pinsNum.length; i++) {
    //Do work here
}

That way you can have your an if statement inside the for loop to switch your loggedon, then check it again outside the for loop:

if(loggedon) {
    //Something happens
}
else {
    //Something also happens
}

Comments

0

The other answers are right that you don't have a proper exit condition. Though a for loop would be more appropriate.

String result = "no logon";
for (String tmp : PinNums) {
    if (tmp.equals(keyPadNumsEntered) {
        result = tmp;
        loggedOn = true;
    }
}

Though even better could just be a simple contains.

if (Arrays.asList(PinNums).contains(keyPadNumsEntered)) {
    result = keyPadNumsEntered;
    loggedOn = true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.