user2875021
user2875021

Reputation: 135

Java ArrayList of ArrayList Scanner UserInput

I am trying to store user input into ArrayList of ArrayList.

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

For example, storing it as [[1,2,3],[4,5],[6,1,8]]
Below is my code,

    Scanner input = new Scanner(System.in);
    System.out.println("Enter a set of numbers:");
    String line = input.nextLine();
    String[] numbers = line.split(" +");

    ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> a1 = new ArrayList<Integer>();

    for(int i=0; i<numbers.length; i++) {
        a1.add(new Integer(numbers[i]));

    }
    a.add(a1);

But then when I types it in to terminal, it becomes [[1,2,3,4,5,6,1,8]].
Thank you in advance!

Upvotes: 0

Views: 525

Answers (5)

Aniket Kulkarni
Aniket Kulkarni

Reputation: 12983

From above comments I am bit clear what do you want to do.

You entered 1 2 3 4 5 6 1 8 on terminal. In your program you used split on - so I am providing some guidelines to solve your problem.

First, split your input on - to make the separation of arrays

String[] numbersArray = line.split("-"); 
//e.g. input 1 2 3 + 4 5 6 + 1 8
//here you will get numbersArray as [1 2 3,4 5 6,1 8]
ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();

Now, to separate individual elements of inner array use for loop

for(int i=0; i<numbersArray.length; i++) {

    String[] numbers= numbersArray[i].split(" "); //split on " "
    //here you will get [1,2,3] for first iteration and 2nd [4,5,6], last [1,8]

    ArrayList<Integer> a1 = new ArrayList<Integer>();
    for(int j=0; j<numbers.length; j++) {
        a1.add(new Integer(numbers[j]));
        //here adding inner array list elements

    }
    //add to main array list 
    a.add(a1);
}

Upvotes: 1

Qadir Hussain
Qadir Hussain

Reputation: 1263

You are adding only one list to a that's why it seems like one element inside list a. If you want to have multiple elements inside list a then use like this.

for(int i=0; i<numbers.length; i++) {
        a1.add(new Integer(numbers[i]));
}
a.add(a1);
a.add(a1);

Output: As per input taken in your question

[[1,2,3,4,5,6,1,8], [1,2,3,4,5,6,1,8]]

Upvotes: 0

SeeTheC
SeeTheC

Reputation: 1631

Try this type of i/p and below code .

i/p : 1 2 3-4 5-6 1 8

String[] list= line.split("-");

ArrayList<ArrayList<Integer>> a = new ArrayList<ArrayList<Integer>>();
for(int i=0; i<list.length; i++) {

        String[] number= line.split(" ");
        ArrayList<Integer> a1 = new ArrayList<Integer>();
        for(int i=0; i<numbers.length; i++) {
            a1.add(new Integer(numbers[i]));

        }
        if(a1.length>0)
           a.add(a1);
}

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

The way you are trying to use, will not work since you add all elements to inner list.

You can try some thing like following. You needs to create following arrays.

public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> a = new ArrayList<>();
    int[] arr1={1,2,3}; // you need to create these arrays
    int[] arr2={4,5};
    int[] arr3={6,1,8};
    a.add(getList(arr1));
    a.add(getList(arr2));
    a.add(getList(arr3));
    System.out.println(a);
}

public static ArrayList<Integer> getList(int[] arr){
    ArrayList<Integer> list = new ArrayList<>();
    for (int anArr : arr) {
        list.add(anArr);
    }
    return list;
}

Upvotes: 0

gaurav aggarwal
gaurav aggarwal

Reputation: 228

I think it's due to the wrong input you are trying to enter on console, because when I hard code the ideal input to your ArrayList it prints the result as you expected. Here is the sample code that I wrote:

private static void doPrint(){
    List<List<Integer>> sample=new ArrayList<List<Integer>>();
    ArrayList<Integer> a1 = new ArrayList<Integer>();
    a1.add(12);
    a1.add(23);
    ArrayList<Integer> a2 = new ArrayList<Integer>();
    a2.add(22);
    a2.add(28);
    sample.add(a1);
    sample.add(a2);

    System.out.println(sample);
}

and here is the output:

[[12, 23], [22, 28]]

Upvotes: 0

Related Questions