Results 1 to 3 of 3
Thread: identical arrays program
- 11-10-2011, 07:10 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 8
- Rep Power
- 0
identical arrays program
I am getting this output with the code below
Enter list1: 5
2
5
6
6
1
Enter list2: 5
5
2
6
1
6
Two lists are identical
I have to press enter twice after the first number and once after each additional number. I need it to allow me to enter all six numbers then press enter for each list. and the output should look like this :
Enter list1: 5 2 5 6 6 1
Enter list2: 5 5 2 6 1 6
Two lists are identical
What do I add to the code to make the output look like the second one. Please Help.
Java Code:package arrays; import java.util.Arrays; public class CheckArrays { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); 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(); System.out.print("Enter list2: "); 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) { if(list1.length == list2.length) { Arrays.sort(list1); Arrays.sort(list2); } else return false; for (int i = 0; i < list1.length; i++) { if (list1[i] != list2[i]) return false; } return true; } }Last edited by pbrockway2; 11-10-2011 at 09:21 AM. Reason: code tags added
- 11-10-2011, 08:20 AM #2
Devil
- Join Date
- Nov 2011
- Location
- Pakistan
- Posts
- 12
- Rep Power
- 0
Re: identical arrays program
Well, use String arrays and get the numbers with the difference value of " ".
- 11-10-2011, 10:05 PM #3
Similar Threads
-
Help with Battleship program! 2D arrays
By tylerkung in forum New To JavaReplies: 3Last Post: 09-23-2011, 01:45 PM -
Converting Program from ArrayLists to Arrays
By Java-Guy in forum New To JavaReplies: 3Last Post: 01-26-2011, 09:02 AM -
Two identical strings are not identical...
By murphaph in forum New To JavaReplies: 7Last Post: 01-05-2010, 09:58 AM -
comparison of two (identical) strings doesn't work
By langaro in forum CLDC and MIDPReplies: 4Last Post: 10-02-2009, 04:02 PM -
Need help with program: arrays
By dangerzone9k in forum New To JavaReplies: 2Last Post: 04-02-2009, 05:59 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks