Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Java Arrays - Entering Lists only let me enter 2 numbers

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Thanked 0 Times in 0 Posts

    Default Java Arrays - Entering Lists only let me enter 2 numbers

    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) {
    	           // 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
    Enter list 2: 2
    Two lists are not identical

  2. #2
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default Re: Java Arrays - Entering Lists only let me enter 2 numbers

    Isn't that what you've written the program to do? And congrats, it does that well.

    If you want to take more values, then you should know about loops. Do you know loops?

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Thanked 121 Times in 105 Posts

    Default Re: Java Arrays - Entering Lists only let me enter 2 numbers

    What you say in your description of your program and what your program does are 2 completely different things.
    Firstly, you dont use any lists at all, you are using arrays. And it will let you enter as many values into your first array as you like, but the first entered value is the number of values you want to store.
    My guess is that you dont understand your own program very well, perhaps you should go over the basics once more and maybe start from scratch. Its not helpful to write random code without understand what one is doing.

Similar Threads

  1. how to count occurance of numbers using java arrays
    By muhammad waqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 1st, 2013, 04:59 PM
  2. printing distinct numbers with arrays
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 16th, 2012, 09:32 AM
  3. Replies: 0
    Last Post: May 10th, 2012, 07:59 PM
  4. help w/ storing/scanning numbers in arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 17th, 2010, 10:55 PM
  5. Conversions of Numbers in Arrays
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 1st, 2010, 07:59 PM

Tags for this Thread