Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Arrays_Exercise {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Array First: ");
- String[] first = sc.nextLine().split(" ");
- System.out.print("Array Second: ");
- String[] second = sc.nextLine().split(" "); // create two arrays through users's input
- String[] output = new String[second.length]; // create a new String array with elements equal to the second array
- int outputIndex = 0; //int index to count the elements in the Array output
- for (int i = 0; i < second.length; i++) { // create nested for cycles to check the arrays for equal elements
- for (int j = 0; j < first.length; j++) {
- if (second[i].equals(first[j])) { // create if condition
- output[outputIndex++] = second[i]; // store the equal symbols in the Array output
- }
- }
- }
- System.out.println(String.join(": ", output)); // Print array output with join
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement