Edris T
Edris T

Reputation: 13

two arrays from user input and get an output of the common values of the two arrays

I'm still new to java and been trying to write code that takes two different arrays of common values and outputs the common values of both arrays but I keep getting the following error message:

Exception in thread "main" Your common values are: 0 Your common values are: 0 Your common values are: 0 Your common values are: 0 java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at HomworkTestingQ.main(HomworkTestingQ.java:18)

        Scanner sc = new Scanner(System.in);{       
            int n = 5;
            int m = 5;
            int[] array1 = new int[m];
            int[] array2 = new int[n];  
        
            System.out.println("Enter the first array: ");
            n=sc.nextInt();
            System.out.println("Enter the second array");
            m=sc.nextInt();
            for(int i = 0; i < array1.length; i++) {
                for(int j = 0; i < array2.length; j++) {
                    if(array1[i] == array2[j]) {
                        System.out.println("Your common values are: " + array1[i+j] );
                    }
                }
            }
        }
    }
}

Upvotes: 1

Views: 1348

Answers (4)

Z fp
Z fp

Reputation: 574

I fix your codes:

        Scanner sc = new Scanner(System.in);

        int n = 5;
        int m = 5;
        int[] array1 = new int[m];
        int[] array2 = new int[n];

        System.out.println("Enter the first array: ");
        for (int i = 0; i < n; i++) {
            array1[i] = sc.nextInt();
        }
        System.out.println("Enter the second array");
        for (int i = 0; i < n; i++) {
            array2[i] = sc.nextInt();
        }
        for (int item : array1) {
            for (int value : array2) {
                if (item == value) {
                    System.out.println("Your common values are: " + item);
                }
            }
        }

Upvotes: 1

Vyom Yadav
Vyom Yadav

Reputation: 145

FIRST PROBLEM
The size of the array won't change when you scan the value of m and n because java is pass by value and the size of the array is the value, not the variable.
So you should do something like-

int m = scanner.nextInt();
int[] arr = new int[m];

SECOND PROBLEM

System.out.println("Your common values are: " + array1[i+j] );

This will go out of bounds, perhaps you should do-

System.out.println("Your common values are: " + array1[i] );

Upvotes: 0

Varun Kedia
Varun Kedia

Reputation: 1

You don't need to add up the indices.. Since array1[i] is equal to array2[j], print any one of them:

for(int i=0;i<array1.length;i++){
    for(int j=i+1;j<array2.length;j++){
        if(array1[i]==array2[j]) int commonValue = array1[i];
        return commonValue; // or print it
    }    
}

Upvotes: 0

MysticZA
MysticZA

Reputation: 101

I believe the issue is that you're adding the array iterators here:

array1[i+j]

The i+j is adding to be more than the length of array1.

An aside, your arrays aren't being populated as I think you expect based on:

        System.out.println("Enter the first array: ");
        n=sc.nextInt();
        System.out.println("Enter the second array");
        m=sc.nextInt();

I'm just speculating there perhaps you have more to do there down the line.

Upvotes: 1

Related Questions