What's wrong with my code? Can somebody help? Thanks!

import java.io.*;

public class Array {
public static void main (String[]args) throws IOException {
BufferedReader myinput=new BufferedReader(new InputStreamReader(System.in));

int Arr1[]=new int[5];
int Arr2[]=new int[5];

int SumArr1=0;
int SumArr2=0;
int x=0;
int y=0;

for (x=0;x<Arr1.length;x++) {
System.out.print("Enter Array1: ");
Arr1[x]=Integer.parseInt(myinput.readLine());
SumArr1=Arr1[x]+SumArr1;
}

System.out.println();

for (y=0;y<Arr2.length;y++) {
System.out.print("Enter Array2: ");
Arr2[y]=Integer.parseInt(myinput.readLine());
SumArr2=Arr2[y]+SumArr2;
}

if (SumArr1==SumArr2) {
System.out.println("The arrays are equal.");
}
else {
System.out.println("The arrays are not equal.");
}

if (Arr1[x]==Arr2[y]) {
System.out.println("The arrays are the same.");
}
else {
System.out.println("The arrays are not the same.");
}

}}

When I have finished input numbers it says:
Enter Array1: 5
Enter Array1: 5
Enter Array1: 5
Enter Array1: 5
Enter Array1: 5

Enter Array2: 5
Enter Array2: 5
Enter Array2: 5
Enter Array2: 5
Enter Array2: 5
The arrays are equal.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Array.main(Array.java:36)

C:\Java\bin>

Recommended Answers

All 4 Replies

If you run by hand both for-loops, you will see that the x,y take value from 0 to 5.
They go: 0, 1, 2, 3, 4, 5. You correctly insert data in the arrays. When the index reaches value 5, then this statement is false:
( y=0; y<Arr2.length; y++ ), So the loop correctly stops:

Array[0] = 5
Array[1] = 5
Array[2] = 5
Array[3] = 5
Array[4] = 5
Then x or y becomes 5 and the loop stops

BUT then you do this:

if (Arr1[x]==Arr2[y])

. The x, y have value 5 and it tries to access the 5 element of the array. But the array can have as index only 0 to 4.

Don't '==' to compare arrays. Use this: Arr1.equals(Arr2). Or create another for-loop that compares each element with each other

Thanks for the explanation. I have fixed the out of bounds but I think the if (Arr1.equals(Arr2)) didn't work. It always says the else part. "The arrays are not the same." even if I have typed the same arrays.

Then create a method in the class that loops the two arrays and checks one by one the elements.

static boolean areEqual(int [] arr1, int [] arr2) {

if (arr1==arr2) return true; 
// if (arr1 != arr2) don't return false. Their elements might be the same

if (arr1.length != arr2.length) return false;
//No need to compare their elements if the arrays don't have the same length

/*
loop through the arrays (don't worry, they have the same length as checked above)
if one of the elements  of one array, 
is not equal with the other's array element then immediately return false 
if the loop finished and false was not returned then all the elements are equal, 
so return true at the end of the method 
*/
}

Use the above method when you want to compare the arrays

> Then create a method in the class that loops the two arrays and checks one by one the
> elements.

Or just use the one provided by the standard library for the Arrays class. If comparing two Object arrays, use the Arrays.deepEquals(Object[] a1, Object[] a2) . When comparing arrays of primitives, use Arrays.equals(primitiveType[] a1, primitiveType[] a2).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.