1

I want to put together a simple program that can take x amount of inputs, put them in an array, do the same thing again in a different array, and then check to see which inputs occur in both lists.

For obtaining the inputs, I can't think of a way for it to continue accepting any number of inputs and only stopping when the input says "stop" or "end" or something similar.

I'm a self-taught beginner, so pardon my ignorance when it comes to this stuff.

I haven't made the setup for the second array, but know how to do so (I'm fairly sure). I'm just stuck on the input issue and how to compare the inputs

//input of codes
    System.out.println("How many codes do you wanna put in?");
    x = input.nextInt();
    int[] binList = new int[x];
    System.out.println("Okay. Enter your codes:");
    for (int n = 0; n < x; n++) {
        binList[n] = input.nextInt();
    }

    //sort codes
    Arrays.sort(binList);

    //display codes
    System.out.println("Here are your codes:");
    for (int n = 0; n < x; n++) {
        System.out.println(binList[n]);
ajc
  • 21
  • 5
  • The problem is that you are using an array. If you used a dynamically sized collection such as `ArrayList` or `Vector`, you won't have any problems with resizing the array. – smac89 Oct 01 '19 at 02:17
  • If you use a `List` then you can loop until you hit a magic number like -1 – Scary Wombat Oct 01 '19 at 02:18
  • For comparing, first check if they are both the same size, and if so loop through checking element-by-element – Scary Wombat Oct 01 '19 at 02:19
  • Your second question sounds like a duplicate of https://stackoverflow.com/questions/5943330/common-elements-in-two-lists – smac89 Oct 01 '19 at 02:21
  • This looks like a good fit for using a `Set` instead of an array - read the values into a a `Set`, then easy to check if the set contains something using the `contains()` method. – Kaan Oct 01 '19 at 03:13

3 Answers3

0

You can do it as follows:

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

public class Test {

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

        // get first list elements
        List<Integer> list1 = getCodes(sc);
        // get second list elements
        List<Integer> list2 = getCodes(sc);

        sc.close();

        // display common codes
        System.out.println("Common elements are: ");
        list1.retainAll(list2);
        for (Integer code : list1) {
            System.out.println(code);
        }
    }

    /**
     * Reads input from console.
     * 
     * @param sc
     * @return
     */
    private static List<Integer> getCodes(Scanner sc) {
        System.out.println("How many codes do you wanna put in?");
        int n = sc.nextInt();
        List<Integer> list = new ArrayList<>();
        System.out.println("Okay. Enter your codes:");
        for (int i = 0; i < n; i++) {
            list.add(sc.nextInt());
        }
        return list;
    }
}
G.G.
  • 592
  • 5
  • 16
  • 1
    Any explanation of what you're doing differently from OP (and why) would greatly improve your answer. – Kaan Oct 01 '19 at 03:08
0

Here's an answer showing how you could find similar array elements using Set. It starts with two int arrays, puts the values into two different Set collections, then walks through one set (doesn't matter which one – my example is walking through setOne) looking for items present in both sets, and finally keeping track of the matches in a new third set named itemsInBothSets.

int[] one = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int[] two = new int[]{2, 4, 6, 8, 10};

Set<Integer> setOne = new HashSet<>();
for (int i : one) {
    setOne.add(i);
}

Set<Integer> setTwo = new HashSet<>();
for (int i : two) {
    setTwo.add(i);
}

Set<Integer> itemsInBothSets = new HashSet<>();
for (Integer integer : setOne) {
    if (setTwo.contains(integer)) {
        itemsInBothSets.add(integer);
    }
}

System.out.println(itemsInBothSets.toString());

When I run this locally, this is the output:

[2, 4, 6, 8, 10]
Kaan
  • 5,434
  • 3
  • 19
  • 41
0

For input taking part:

to continue accepting any number of inputs and only stopping when the input says "stop" or "end" or something similar

For your case, it is better to use arraylist in stead of array as you are taking input for x (unknown) number of items. Following program takes input from user until -1 is entered:

ArrayList<Integer> binList = new ArrayList<>();
System.out.println("Enter your codes:");
while (true) {
    x = input.nextInt();
    if (x == -1) break;
    binList.add(x);
}

For comparison part:

I assume that you have already created two arraylists, binList & binList2. I also assume that you have already taken input for variable number of items in both of the arraylists as stated above. Now for comparing them, use following code:

for (int i = 0; i < binList.size(); i++) {
    for (int j = 0; j < binList2.size(); j++) {
        if(binList.get(i) == binList2.get(j)) {
            System.out.print(binList.get(i) + " ");
        }
    }
}

Note that, the size() method returns the size of the arraylist. Here's the sample input and output for above codes:

Sample input:

Enter your codes:

1 2 3 4 -1

Enter your codes:

2 4 5 7 9 -1

Sample output:

2 4
UkFLSUI
  • 5,509
  • 6
  • 32
  • 47