Hi there! I am trying to write a code that allows you to input 2 lists and it tells you if they are identical or not. However, when I enter my fist list I can only enter two values and then it asks for the next list. How would I fix this to allow me to enter more than two values?
Also, If the second list is different it has me enter more values that list one.
public static void main(String[] args) { Scanner input = new Scanner(System.in); // Enter values for list1 System.out.print("Enter list1: "); int size1 = input.nextInt(); int[] list1 = new int[size1]; for (int i = 0; i < list1.length; i++) list1[i] = input.nextInt(); // Enter values for list2 System.out.print("Enter list 2: "); int size2 = input.nextInt(); int[] list2 = new int[size2]; for (int i = 0; i < list2.length; i++) list2[i] = input.nextInt(); if (equal(list1, list2)) { System.out.println("Two lists are identical"); } else { System.out.println("Two lists are not identical"); } } public static boolean equal(int[] list1, int[] list2) { boolean isEqual = false; if(list1.length == list2.length) { Arrays.sort(list1); Arrays.sort(list2); // Don't return quite yet for (int i = 0; i < list1.length; i++) { if (list1[i] != list2[i]) { isEqual = false; } else isEqual = true; } } return isEqual; } }
My output looks like this:
Enter list1: 1
2
Enter list 2: 2
3
2
Two lists are not identical