0

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!

8
  • 1
    How did you input the numbers? I mean tell us where did you use +? Commented Oct 13, 2014 at 4:49
  • You created two ArrayList objects. What other output would you expect? Commented Oct 13, 2014 at 4:50
  • 1
    Please provide details about your input. you have coded only for one ArrayList. Commented Oct 13, 2014 at 4:50
  • basically i typed into the terminal by entering spacebar between numbers. eg 1 2 3 4 5 6 1 8 on one single line Commented Oct 13, 2014 at 4:53
  • How will you identify which is the first array list and which is second ? Put some separator. Commented Oct 13, 2014 at 4:54

5 Answers 5

1

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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

however, when i am trying using your method, entering to the terminal like 1 2 3 + 4 5 6 + 1 8 i got a Runtime Error with NumberFormatException. not quite sure why thou.
I have figured it out. Instead of using ("+"), I have used ("-") and works like a charm.
@user2875021 : Fine. It's GOOD to see you have solved the problem. SO is always there to help you.
1

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);
}

Comments

0

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]]

Comments

0

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;
}

Comments

0

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]]

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.