0
Scanner input = new Scanner(System.in);
System.out.println("Number of Array lists");
int total_arraylists = input.nextInt();
ArrayList<Integer> lists[]=new ArrayList[total_arraylists];
for( int i = 0; i < total_arraylists; i++){   
    lists[i]=new ArrayList<Integer>(i);
    System.out.println("Enter the values");
    while(input.hasNextInt()){
        lists[i].add(input.nextInt());
    }
    System.out.println(lists[i]);
}

Output of the above program is:

Number of Array lists
3
Enter the values
1
2
3
done
[1, 2, 3]
Enter the values
[]
Enter the values
[]

As we can see, when I enter any character or string (in this case,I entered "done"), the while loop exits and the other 2 Array lists remain empty. I want to add int values to those remaining Array lists too. How can I do it?

1
  • Do you want to enter the same value [1,2,3] to all the ArrayLists? Commented Jul 14, 2020 at 7:18

4 Answers 4

2

You need an input.next(); after the inner loop to "eat up" the "done" response:

        for( int i = 0; i < total_arraylists; i++)
        {   lists[i]=new ArrayList<Integer>(i);
            System.out.println("Enter the values");
            while(input.hasNextInt())
            {
                lists[i].add(input.nextInt());
            }
            input.next();  //<----- Get past the "done"
            System.out.println(lists[i]);
        }

Otherwise, when you go back to get the data for the next list, the input.hasNextInt() will see that the word "done" is waiting to be read; "done", of course, is not an int so hasNextInt() will immediately return false. Doing input.next(); is a convenient way to read past that waiting input.

Sign up to request clarification or add additional context in comments.

2 Comments

Its not working. When I write "done", it prints the 1st Arraylist sucessfully but print the rest of the Arraylists blank i.e [ ].
My bad: input.nextLine() actually only gets past the end-of-line following the last number, but leaves "done" (which is on the next line) unread. Using input.next() instead not only reads past that end-of-line but then goes on to consume the "done". I've revised my example.
0
can you explain what is your requirement? Is it you want to just see how to read and add values in list or just to put some random values, check if this helps you.

System.out.println("Number of Array lists");
        int total_arraylists = input.nextInt();
        ArrayList<Integer> lists[] = new ArrayList[total_arraylists];
        ArrayList<Integer> list = null;
        for( int i = 0; i < total_arraylists; i++) {   
            list = new ArrayList<Integer>();
            for(int j=1; j<=3; j++) {
                list.add(j);
            }
            lists[i] = list;
        }
        System.out.println("----- result-----");
        for(int i =0 ;i< total_arraylists;i++) {
            System.out.println(lists[i]);
        }


Output::

Number of Array lists
3
----- result-----
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]

Comments

0

Your hasNextInt() is always returning true. So all the numbers gets added to your first list.

import java.io.*;
import java.util.*;

class Hello{
    public static void main(String args[]){
        Scanner input = new Scanner(System.in);
        System.out.println("Number of Array lists");
        int total_arraylists = input.nextInt();
        ArrayList<Integer> lists[]=new ArrayList[total_arraylists];
        for( int i = 0; i < total_arraylists; i++){   
            lists[i]=new ArrayList<Integer>(i);
            System.out.println("Enter the values");
            int array_length = input.nextInt();
            while(array_length-- > 0){
                lists[i].add(input.nextInt());
            }
            System.out.println(lists[i]);
        }
    }
}

Input:

3
3
1
2
3
2
4
9
2
9
12

Output:

Number of Array lists
Enter the values
[1, 2, 3]
Enter the values
[4, 9]
Enter the values
[9, 12]

Comments

0

Add a input.nextLine() before and after the while loop:

for( int i = 0; i < total_arraylists; i++){   
    lists[i]=new ArrayList<Integer>(i);
    System.out.println("Enter the values");
    input.nextLine();
    while(input.hasNextInt()){
        int inputNumber = input.nextInt();
        lists[i].add(inputNumber);
    }
    input.nextLine();
    System.out.println(lists[i]);
}

Output:

Number of Array lists
3
Enter the values
1
2
3
done
[1, 2, 3]
Enter the values
1
2
3
done
[1, 2, 3]
Enter the values
1
2
3
done
[1, 2, 3]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.