简体   繁体   中英

Can't fix error of Array Index Out of Bounds

The code that the error is pointing to is:

letters[addr(tmp.charAt(0))] = Integer.parseInt(tmp.substring(2,tmp.length()))

And it is initialized in main:

int letters[] = new int[26]; //holds variables
for(int i = 0; i < 26; i++)
    {
    letters[i] = i+1;
    }

The error says that the string Index is out of bounds: -1. I am pretty sure that it is not the control that is doing this, as when i commented out control it did the same thing. I am trying to figure out how to fix this, and would appreciate any assistance.

just place an if condition before as follows

String tmp = scanner.next();

if(!tmp.length()<=1)
{
letters[addr(tmp.charAt(0))] = Integer.parseInt(tmp.substring(2,tmp.length()))\
}

else
{
    //do something to handle ArrayOutOfBondsException
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM