Let the user input string in array then find the common elements
I keep on encountering error, I can't find the common elements in array.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class number2 {
public static void main(String[] args) {
List<String> l = new ArrayList();
System.out.println("Enter First countries: ");
Scanner Country1 = new Scanner(System.in);
String a = Country1.nextLine();
l.add(a);
List<String> m = new ArrayList();
System.out.println("Enter Second Countries: ");
Scanner Country2 = new Scanner(System.in);
String b = Country2.nextLine();
m.add(b);
System.out.println("First countries: " + l);
System.out.println("Second Countries: " + m);
System.out.println("Common country/s: " + l.retainAll(m));
}
}
The user is supposed to enter a list of countries, for example:
japan,korea,china
And same for second list, for example:
korea,chile,mexico
The output is []
(an empty array) when it should be [korea]
.