Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago