Advertisement
Kancho

Compare_Two_Arrays

Mar 8th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 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. String[] first = sc.nextLine().split(" ");
  8. String[] second = sc.nextLine().split(" "); // take the user's input and make two Arrays
  9.  
  10. for (int i = 0; i < second.length; i++) {
  11. for (int j = 0; j < first.length; j++) { // read the input of the two arrays
  12. if (second[i].equals(first[j])) { //create IF condition to check for equal elements
  13. if (i < second.length - 1) { // create condition to check whether the symbol is last
  14. System.out.print(second[i] + ", "); // if not print the symbol + comma d space
  15. }else { // else print the symbol without comma and space
  16. System.out.print(second[i]);
  17. }
  18. }
  19.  
  20.  
  21. }
  22.  
  23. }
  24.  
  25.  
  26. }
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement