Gurkirat Singh
Gurkirat Singh

Reputation: 33

How to add data in Multiple Array lists?

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?

Upvotes: 0

Views: 113

Answers (4)

Kevin Anderson
Kevin Anderson

Reputation: 4592

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.

Upvotes: 2

Marc
Marc

Reputation: 3245

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]

Upvotes: 0

cegprakash
cegprakash

Reputation: 3125

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]

Upvotes: 0

Panish Pawade
Panish Pawade

Reputation: 1

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]

Upvotes: 0

Related Questions