Home > OS >  I keep on encountering error, I can't find the common elements in array
I keep on encountering error, I can't find the common elements in array

Time:03-19

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].

CodePudding user response:

Notes after the code.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class number2 {

    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);

        List<String> l = new ArrayList<>();
        System.out.println("Enter first countries separated by comma: ");
        String a = stdin.nextLine();
        String[] countries1 = a.split(",");
        l.addAll(Arrays.asList(countries1));

        List<String> m = new ArrayList<>();
        System.out.println("Enter second countries separated by comma: ");
        String b = stdin.nextLine();
        String[] countries2 = b.split(",");
        m.addAll(Arrays.asList(countries2));

        System.out.println("First countries: "   l);
        System.out.println("Second Countries: "   m);
        l.retainAll(m);
        System.out.println("Common country/s: "   l);
    }
}
  • You don't need to create a new Scanner each time you want to get input from the user. The above code creates a single Scanner and assigns it to variable stdin.
  • When assigning a value to variables l and m, you should use the diamond operator.
  • You say that the user is supposed to enter a list of countries separated by commas. Hence, in order to get a List, you need to call method split, of class java.lang.String, on the user-entered string and then create a List by calling method addAll, of interface java.util.List since method split returns an array. You need to do this for both lists of countries that the user enters. Note that in the above code, the countries must be separated by a single comma without spaces. If you want a different delimiter you need to change the argument to method split. The argument is a regular expression.
  • Finally you can call method retainAll. However that method returns boolean and modifies the List. So after the method call, i.e. l.retainAll(m), l will contain only the common countries.

Here is output from a sample run.

Enter first countries separated by comma: 
japan,korea,china
Enter second countries separated by comma: 
korea,chile,mexico
First countries: [japan, korea, china]
Second Countries: [korea, chile, mexico]
Common country/s: [korea]

CodePudding user response:

You should split the entries into your arrayList l = new ArrayList<>(Arrays.asList(a.split(" "))); :

For the common list you can use this :

    //edit to list the common element using stream 
    l = l.stream().filter(m::contains).collect(Collectors.toList());
    //or this one
    //--- l.retainAll(m);
    System.out.println("Common country/s: " l);

Example:

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 = new ArrayList<>(Arrays.asList(a.split(" ")));

    List<String> m=new ArrayList<String>();
    System.out.println("Enter Second Countries: ");
    Scanner Country2=new Scanner(System.in);
    String b = Country2.nextLine();
    m = new ArrayList<>(Arrays.asList(b.split(" ")));

    System.out.println("First countries: " l);
    System.out.println("Second Countries: " m);

    //edit to list the common element using stream 
    l = l.stream().filter(m::contains).collect(Collectors.toList());
    //or this one
    //--- l.retainAll(m);

    System.out.println("Common country/s: " l);

}

Output :

Enter First countries: 
korea japan china
Enter Second Countries: 
japan china usa
First countries: [korea, japan, china]
Second Countries: [japan, china, usa]
Common country/s: [japan, china]
  • Related