Advertisement
Kancho

New_Compare_Two_Arrays

Mar 8th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Arrays_Exercise {
  4. public static void main(String[] args) {
  5. Scanner sc = new Scanner(System.in);
  6.  
  7. System.out.print("Array First: ");
  8. String[] first = sc.nextLine().split(" ");
  9. System.out.print("Array Second: ");
  10. String[] second = sc.nextLine().split(" "); // create two arrays through users's input
  11.  
  12. String[] output = new String[second.length]; // create a new String array with elements equal to the second array
  13. int outputIndex = 0; //int index to count the elements in the Array output
  14.  
  15. for (int i = 0; i < second.length; i++) { // create nested for cycles to check the arrays for equal elements
  16. for (int j = 0; j < first.length; j++) {
  17. if (second[i].equals(first[j])) { // create if condition
  18. output[outputIndex++] = second[i]; // store the equal symbols in the Array output
  19. }
  20. }
  21.  
  22. }
  23. System.out.println(String.join(": ", output)); // Print array output with join
  24. }
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement