Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Arrays;
- public class SoftUni {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter the first line of elements: ");
- String first = sc.nextLine();
- String[] firstArray = first.split(" ");
- System.out.print("Enter the second line of elements: ");
- String second = sc.nextLine();
- String[] secondArray = second.split(" ");
- //setting for-loop limits
- int arraySize;
- int arraySize2;
- if(firstArray.length > secondArray.length){
- arraySize = firstArray.length;
- arraySize2 = secondArray.length;
- }
- else{
- arraySize = secondArray.length;
- arraySize2 = firstArray.length;
- }
- String endString = "";
- for(int i = 0;i<arraySize;i++){
- for(int j = 0;j<arraySize2;j++){
- if(firstArray[i].equals(secondArray[j])){
- endString += firstArray[i] + " ";
- }
- }
- }
- System.out.println(endString);
- }
- }
Add Comment
Please, Sign In to add comment