Reputation:
I have one main class where the user is asked to write a number (int num). This number will be the size of an array in another class. I will populate that array using the Math.random.
This is the Main class:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Write amount of numbers between 0-99: ");
int num = input.nextInt();
IckeMain im = new IckeMain();
im.setNum(num);
im.fillArray();
}
This is the other class:
public class IckeMain {
Scanner inread = new Scanner (System.in);
int num;
int [] array = new int [num];
int [] nyArr = new int [num];
public void fillArray () {
System.out.println("You chose: " + num + " with size: " +array.length);
for (int j=0; j<num; j++) {
array[j] = (int)(Math.random() * 99);
System.out.println("Unsorted: " +array[j]);
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
Now, let's say that I type "5" in the main class.. this is the output that I'll get.
Write amount of numbers between 0-99:
5
You chose: 5 With size: 0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
So, apparently the variable num, is set to the number specified by the user. But, somehow, the array can't seem to "get" that number considering it prints 0. Why is that?
Upvotes: 1
Views: 1917
Reputation: 11
At the time of initialization of array value for num is 0 . that's why you are getting Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
I have changed your code as below: Main class
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Write amount of numbers between 0-99: ");
int num = input.nextInt();
IckeMain im = new IckeMain(num);
im.setNum(num);
And IckeMain class
`
public class IckeMain {
int num;
int [] array;
int [] nyArr ;
public IckeMain(int num) {
array = new int [num];
nyArr = new int [num];
}
public void fillArray () {
System.out.println("You chose: " + num + " with size: " +array.length);
for (int j=0; j<num; j++) {
array[j] = (int)(Math.random() * 99);
System.out.println("Unsorted: " +array[j]);
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
`
Upvotes: 0
Reputation: 1060
Here is some need full changes
public class IckeMain {
Scanner inread = new Scanner (System.in);
int num;
int [] array ;
int [] nyArr ;
/* this will initialize with 0 size since the default value of int is 0 means num=0
int [] array = new int [num];
int [] nyArr = new int [num];*/
public void fillArray () {
array = new int [num];
nyArr = new int [num];
System.out.println("You chose: " + num + " with size: " +array.length);
for (int j=0; j<num; j++) {
array[j] = (int)(Math.random() * 99);
System.out.println("Unsorted: " +array[j]);
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
Upvotes: 1
Reputation: 317
You'll want to take advantage of a constructor as part of object oriented programming to handle this. What's happening is that the code to set the size of the array is being ran when you first start the program. Then when you call the method, the array is already created with size zero. I presume that you are not proficient with objects, however, so I will provide a workaround. Though, I would recommend reading up on object oriented design as it is a very powerful tool.
Igor's answer utilizes a constructor and object oriented programming, however if this is not something you are familiar with then I would caution your use of it at the moment.
In your setNum()
method, you should reinitialize the array, rather than up at the top. This will solve your issue.
public class IckeMain {
Scanner inread = new Scanner (System.in);
int num;
//Make sure to declare your array up here, but don't worry about setting a value
int [] array;
int [] nyArr;
public void fillArray () {
System.out.println("You chose: " + num + " with size: " +array.length);
for (int j=0; j<num; j++) {
array[j] = (int)(Math.random() * 99);
System.out.println("Unsorted: " +array[j]);
}
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
//Now initialize to a new array with size num
array = new int [num];
nyArr = new int [num];
}
Upvotes: 1
Reputation: 912
The problem is that you are creating arrays before you know their sizes.
I would change you code to this:
public class IckeMain {
Scanner inread = new Scanner (System.in);
int num;
int [] array;
int [] nyArr;
public IckeMain(int num) {
this.num = num;
}
public void fillArray () {
array = new int [num];
nyArr = new int [num];
System.out.println("You chose: " + num + " with size: " +array.length);
for (int j=0; j<num; j++) {
array[j] = (int)(Math.random() * 99);
System.out.println("Unsorted: " +array[j]);
}
}
public int getNum() {
return num;
}
}
Here you will need to pass num that is size of the arrays in the constructor of IckeMain. So, instead of
IckeMain im = new IckeMain();
im.setNum(num);
you will need to have only one line:
IckeMain im = new IckeMain(num);
I hope this helps.
Upvotes: 1
Reputation: 1941
This is because your array declarations -
int [] array = new int [num];
int [] nyArr = new int [num];
happen with num = 0. These initializations do not happen after you have set the num value to 5. Hence the discrepancy and the ArrayIndexOutOfBoundsException
for index 0 itself as the arrays declared are actually empty, with no size at all.
You can do
array = new int[num];
nyArr = new int [num];
in the setNum() method, so that the new value of num is reflected in the array size.
Upvotes: 1