0

I am having an array error, saying it is out of bounds I have no idea what is wrong. Here is my code:

import java.util.*;
public class gameVar {
    public static int size;
    public static int counter;
    public static Scanner input = new Scanner(System.in);
    public static String currentIn;
    public static String nameArray[] = new String[size];
}

and the second class(where i am getting the error on line 6):

public class mainThread extends gameVar {
public static void main(String[] args){
    System.out.println("Please type the desired amount of players: ");
    size = input.nextInt();
    for(int counter = 0; counter < size; counter++){
        System.out.println("Please enter the name of player " + nameArray[counter])
        }
    }
}

Your help is much appreciated!

1
  • Are you sure nameArray[counter] is a valid value for every value of counter in the loop? Commented Mar 24, 2013 at 8:17

7 Answers 7

1

The following allocates a zero-element array:

public static int size;
public static String nameArray[] = new String[size]; // <<<< Here, `size` is zero

You need to move array initialization into main():

public static void main(String[] args){
    System.out.println("Please type the desired amount of players: ");
    size = input.nextInt();
    nameArray = new String[size]; // <<< THIS
    for(int counter = 0; counter < size; counter++){
        System.out.println("Please enter the name of player " + nameArray[counter])
        }
    }
}

You can then remove the = new String[size] from the declaration of nameArray.

Sign up to request clarification or add additional context in comments.

Comments

0

You did not reinitialize you array class after getting the size you want for it. Your design as a whole needs some work, as you are overly relying on static (global) state.

In gameVar:

public static int size; // <-- by default this is zero
public static String nameArray[] = new String[size]; // <-- Initialized here to size zero!

In mainThread:

size = input.nextInt(); // <-- size is no longer zero
for(int counter = 0; counter < size; counter++) {
  System.out.println("Please enter the name of player " + nameArray[counter]); // <-- But your array is still size zero!
}

The simple fix would be to do this after getting the new size:

nameArray = new String[size];

But as I mentioned before, you should rethink your design (design a proper class without static variables).

Comments

0

In your field declarations, when you set public static int size; Java defaults size to 0. So when you create your String array, the size of the array is 0.

In general it is not advisable create new objects in the field declarations. Rather, just have

public static String nameArray[];

then set nameArray to a new String array AFTER you know what the size will be.

size = input.nextInt();
nameArray[] = new String[size];
for(......

Comments

0

The value of size is zero by defualt. So the array is created of size 0. Eg : public static String nameArray[] = new String[0];

Hence you get the exception. Assign some value to the variable size.

1 Comment

You can rate the answer if it has helped you. :-)
0

your MainThread class should be like this:

public class mainThread extends gameVar {
public static void main(String[] args){
    System.out.println("Please type the desired amount of players: ");
    size = input.nextInt();
    nameArray = new String[size];//reinitialize the nameArray here.
    for(int counter = 0; counter < size; counter++){
        System.out.println("Please enter the name of player " + nameArray[counter]=input.next())
        }
    }
}

Comments

0

Array size is static in nature, nameArray is declared with size zero as static size variable has no initialization, it would get default 0.

so nameArray[counter] It must populate Array out of bound exception.

You must initialize array with right size.

nameArray = new String[size];

Comments

0

when you initialize the array the size of it is set to 0.

public static String nameArray[] = new String[size];

if you change the value of size latter size = input.nextInt();

it change the value of the integer variable size but doesn't change the size of the array, that why you getting the error. you can check it by printing the size of array (nameArray.length) latter.

you need to initialize it after getting the value of 'size'.

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.