Reputation: 11
im getting a out of bounds error on line 57. any help is appreciated. the program takes 1 integer and creates and array with that many indexes. it then asks for more integers to fill it up. it then takes all the even numbers and places it into a second array with is then outputted.
run:
Enter the ammount of integers you will require.
10
Enter your integers:
1
Enter your integers:
2
Enter your integers:
3
Enter your integers:
4
Enter your integers:
5
Enter your integers:
6
Enter your integers:
7
Enter your integers:
8
Enter your integers:
9
Enter your integers:
0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at AllEven.arrcheck2(AllEven.java:57)
at AllEven.main(AllEven.java:24)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
CODE
import java.util.*;
public class AllEven {
public static void main(String[] args) {
int read;
Scanner kybd = new Scanner(System.in);
System.out.println("Enter the ammount of integers you will require.");
read = kybd.nextInt();
int[] arr1 = new int[read];
int[] arr2;
int count = 0;
arrRead(arr1);
arrcheck(arr1);
arr2 = new int[count];
arrcheck2(arr1, arr2);
mainPrint(arr2);
}
public static int arrcheck(int[] arr1) {
int count = 0;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] % 2 == 0) {
count++;
}
}
return count;
}
public static void arrRead(int[] arr1) {
Scanner kybd = new Scanner(System.in);
for (int i = 0; i < arr1.length; i++) {
System.out.println("Enter your integers: ");
arr1[i] = kybd.nextInt();
}
}
public static void arrcheck2(int[] arr1, int[] arr2) {
int j = 0;
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] % 2 == 0) {
arr2[j] = arr1[i];
j++;
}
}
}
public static void mainPrint(int[] arr2) {
for (int i = 0; i < arr2.length; i++) {
System.out.println("Printing Even Numbers.");
System.out.println(arr2[i]);
}
}
}
ok so thanks everyone who helped, But now after the integers have been inputted the program just finishes without displaying anything after.
Upvotes: 1
Views: 172
Reputation: 31222
You are not initializing your array int[] arr2
. You should not use an array to populate even elements from the initial array because arrays require a determined size upon initialization -- you are better off using ArrayList
and then conver it into an array once you know the definitive size, if you must.
Upvotes: 1
Reputation: 213223
You need to assign the return value of arrcheck
invocation in count variable before creating arr2
. Otherwise, you would create your arr2
with size 0.
Change: -
int count = 0;
arrRead(arr1);
arrcheck(arr1);
to: -
arrRead(arr1);
int count = arrcheck(arr1);
Upvotes: 2
Reputation: 26058
For starters you have your arrcheck
function returning a count and not doing anything with it. I suspect you meant to write something like this:
arrRead(arr1);
int count = arrcheck(arr1);
otherwise your next line:
arr2 = new int[count];
creates an array of size 0 which will cause array out of bounds as soon as it is accessed.
Also you spelled Amount wrong, I think the rest of the program should work with the above change.
Upvotes: 1
Reputation: 3185
You need to initialize arr2 to the same length as arr1. int count = read will fix the issue. Or you can remove count entirely and just use read when initializing the array.
read = kybd.nextInt();
int[] arr1 = new int[read];
int[] arr2;
int count = read;
arrRead(arr1);
arrcheck(arr1);
arr2 = new int[count];
Upvotes: 1
Reputation: 950
You create an array with length 0.
arr2 = new int[count];
You should give it a length bigger than 0.
If you want it to be the same length as arr1 just do arr2 = new int[read];
Upvotes: 1
Reputation: 18286
The size of arr2 is 0. so when you try to access a non existing index in it you get the exception.
Upvotes: 1
Reputation: 5782
You are defining arr2
as a zero-length array:
int count = 0;
....
arr2 = new int[count];
Then, in arrcheck2
you have the line:
arr2[j] = arr1[i];
This will not work because arr2
has no length so you cannot put anything into it.
Upvotes: 1
Reputation: 5849
After brief look at the code I think your problem lies here:
int count = 0;
arrRead(arr1);
arrcheck(arr1);
arr2 = new int[count]; //array of size 0!
arrcheck2(arr1, arr2);
Upvotes: 1