简体   繁体   中英

Not sure why array goes out of bounds JAVA

I'm getting an Array Index Out of Bounds exception on the String weapon = dataz[1]; line at runtime. I'm not sure what is causing this as this is nearly the same code I've used on previous assignments. Any logic as to why this is happening would be greatly appreciated!

public Hero[] getHeroes(){

    String file = getFilePath();
    Hero[] heroPower = new Hero[5];

    int i=0;
    try{
    Scanner data = new Scanner(file);
    while(data.hasNextLine() && i < 5)
    {
        String next = data.nextLine();
            if(!next.trim().isEmpty())
            {
                String[] derp = next.split(",");
                String name = derp[0];
                String weapon = derp[1];
                int attackPoints = Integer.parseInt(derp[2]);
                heroPower[i] = new Hero(name,weapon,attackPoints);
                i++;
            }
        }
     data.close();
    } finally {

    }   
    return heroPower;
  }
}

Your next string probably doesn't split. It doesn't have a , and you're not checking for that option.

Your code processes empty lines correctly, but it fails when the input does not have at least three tokens: it assumes that derp[0] , derp[1] , and derp[2] are valid, but the validity depends on the input.

You can fix it by checking the number of tokens that you get back from next.split :

String next = data.nextLine();
String[] derp = next.split(",");
if(derp.length >= 3) {
    ...
}

This condition also covers the situation when the trimmed next is empty, so a separate check is not required.

You really need to make sure that the number of inputs you are inputting is the number of inputs you are expecting, a simple check would be to check the number of arguments in the derp array that you get from split.

public Hero[] getHeroes(){

    String file = getFilePath();
    Hero[] heroPower = new Hero[5];

    int i=0;
    try{
    Scanner data = new Scanner(file);
    while(data.hasNextLine() && i < 5)
    {
        String next = data.nextLine();
            if(!next.trim().isEmpty())
            {
                String[] derp = next.split(",");
                //This is the line to change
                if(derp > 3){
                    String name = derp[0];
                    String weapon = derp[1];
                    int attackPoints = Integer.parseInt(derp[2]);
                    heroPower[i] = new Hero(name,weapon,attackPoints);
                    i++;
                 }else{

                     //throw an error

                 }
            }
        }
    data.close();
    } finally{

    }   
    return heroPower;
}
}

The problem is most likely your input, it doesn't contain any , symbols:

String[] derp = next.split(","); // split by commas a word that has no commas so derp.length == 1
String name = derp[0];           // this is ok since length is 1
String weapon = derp[1];         // this is error

You should check derp.length before using it:

String[] derp = next.split(",");
if(!derp.length == 3) { // because name=derp[0], weapon=derp[1], attackPoints = derp[2]
    // ... create name, weapon, points and assign to heroPower
} else {
    System.out.println("Invalid input");
}

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