Orion447
Orion447

Reputation: 400

Index of String array

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";
        }
    }
}

Upvotes: 1

Views: 71

Answers (5)

mattmess1221
mattmess1221

Reputation: 4434

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

Upvotes: 0

kay
kay

Reputation: 366

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";
    }
}

Upvotes: 1

Hydron
Hydron

Reputation: 95

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
}

Upvotes: 0

Ashu
Ashu

Reputation: 2266

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";
        }
    }

Upvotes: 0

Mureinik
Mureinik

Reputation: 311103

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

Upvotes: 1

Related Questions