Question: can anyone help me fix my code and tell me where is my fault ? java code this is my code import java.util.ArrayList; import java.util.HashMap;

can anyone help me fix my code and tell me where is my fault ?

java code

this is my code

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Scanner;

public class Main {

public static void main(String a[])

{

Scanner sc = new Scanner(System.in);

ArrayList list1 = new ArrayList();

ArrayList list2 = new ArrayList();

System.out.println("Enter the size of list1 ");

int n1= sc.Int1();

System.out.println("Enter the size of list 2: ");

int n2 = sc.Int2();

System.out.println("Enter first array elements: ");

for(int i = 0; i < n1; i++){

int num = sc.Int1();

list1.add(num);

}

System.out.println("Enter second array elements: ");

for(int i = 0; i < n2; i++){

int num1 = sc.Int2();

list2.add(num);

}

HashMap mp = new HashMap();

for(int i = 0; i < n1; i++){

mp.put(list1.get(i), 1);

}

for(int i = 0; i < n2; i++){

mp.put(list2.get(i), 2);

}

System.out.println(" common elements: ");

for(int i = 0; i < n; i++){

if(mp.containsKey(list2.get(i)))

System.out.print(list2.get(i) + " ");

}

System.out.println();

}

}

i want to do is like this

"Enter the size of list1 ");

4

"Enter the size of list2

6

"Enter first array elements:

1 2 3 4

"Enter 2nd array elements:

2 3 4 5 6 7

common elements:

2 3 4

thanks

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!