Results 1 to 3 of 3
  1. #1
    grcarr77 is offline Member
    Join Date
    Oct 2011
    Posts
    8
    Rep Power
    0

    Default 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

  2. #2
    Mr777 is offline Devil
    Join Date
    Nov 2011
    Location
    Pakistan
    Posts
    12
    Rep Power
    0

    Default Re: identical arrays program

    Well, use String arrays and get the numbers with the difference value of " ".

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: identical arrays program

    Quote Originally Posted by grcarr77 View Post
    What do I add to the code to make the output look like the second one.
    Nothing. Simply only hit enter once after the last digit is typed. So the input would be: 5space2space5space6space6space1enter

Similar Threads

  1. Help with Battleship program! 2D arrays
    By tylerkung in forum New To Java
    Replies: 3
    Last Post: 09-23-2011, 01:45 PM
  2. Converting Program from ArrayLists to Arrays
    By Java-Guy in forum New To Java
    Replies: 3
    Last Post: 01-26-2011, 09:02 AM
  3. Two identical strings are not identical...
    By murphaph in forum New To Java
    Replies: 7
    Last Post: 01-05-2010, 09:58 AM
  4. comparison of two (identical) strings doesn't work
    By langaro in forum CLDC and MIDP
    Replies: 4
    Last Post: 10-02-2009, 04:02 PM
  5. Need help with program: arrays
    By dangerzone9k in forum New To Java
    Replies: 2
    Last Post: 04-02-2009, 05:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •