Pedro Martins
Pedro Martins

Reputation: 125

Get elements from line of integers java

I have the following

10 30 15
89 1 3
65 48 12

I would like to have each of those numbers in each line to different lists.

For example

list1 contains 10 30 15
list2 contains 89 1 3
list3 contains 65 48 12

I have tried the following but without sucess... Could someone help me out?

    ArrayList<Integer> list1 = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    ArrayList<Integer> list3 = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    while(sc.hasNextLine()) {
        list1.add(sc.nextInt());
    }
    while(sc.hasNextLine()) {
        list2.add(sc.nextInt());
    }
    while(sc.hasNextLine()) {
        list3.add(sc.nextInt());
    }

    sc.close();

Upvotes: 2

Views: 413

Answers (4)

humblefoolish
humblefoolish

Reputation: 429

You need to split the next line using space as the delimiter(if that is always the case) then use Integer.parseInt() on those tokens to add elements on the list.

public static void main(String... args){
    List<Integer> list1 = new ArrayList<>();
    List<Integer> list2 = new ArrayList<>();
    List<Integer> list3 = new ArrayList<>();

    Scanner sc = new Scanner(System.in);
    String[] strArray;

    if(sc.hasNextLine()) {
        strArray = sc.nextLine().split("\\s+");
        for(String item : strArray)
            list1.add(Integer.parseInt(item));
    }
    if(sc.hasNextLine()) {
        strArray = sc.nextLine().split("\\s+");
        for(String item : strArray)
            list2.add(Integer.parseInt(item));
    }
    if(sc.hasNextLine()) {
        strArray = sc.nextLine().split("\\s+");
        for(String item : strArray)
            list3.add(Integer.parseInt(item));
    }

    sc.close();
    System.out.println(list1);
    System.out.println(list2);
    System.out.println(list3);
}

Console:

//input
10 30 15
89 1 3
65 48 12

   //output
  [10, 30, 15]
  [89, 1, 3]
  [65, 48, 12]

This is assuming there are always 3 lines of input(as OP stated) and no bad input.

Upvotes: 2

arthur
arthur

Reputation: 3325

Each line needs to be splited and then each part of the split needs to be parsed:

Set<List<Integer>> myListSet = new HashSet<>();
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine()) {
            List<Integer> intList = new ArrayList<>();
            Arrays
                .stream(sc.nextLine().split("\\s+"))
                .map(s -> Integer.parseInt(s))
                .forEach(intList::add);
            myListSet.add(intList);
        }
        sc.close();

myListSet now contains many lists and each list has n integer Numbers

Upvotes: 0

Kepotx
Kepotx

Reputation: 1082

As you use scanner, you can use the method nextInt() and hasNextInt()

    ArrayList<Integer> list1 = new ArrayList<Integer>();
    ArrayList<Integer> list2 = new ArrayList<Integer>();
    ArrayList<Integer> list3 = new ArrayList<Integer>();
    Scanner sc = new Scanner(System.in);
    while(sc.hasNextLine()) {
        while (sc.hasNextInt()) {
        list1.add(sc.nextInt());
        }
    }
    sc.nextLine();
    while(sc.hasNextLine()) {
        while (sc.hasNextInt()) {
        list2.add(sc.nextInt());
        }
    }
    sc.nextLine();
    while(sc.hasNextLine()) {
        while (sc.hasNextInt()) {
        list3.add(sc.nextInt());
        }
    }

    sc.close();

Upvotes: 0

vincrichaud
vincrichaud

Reputation: 2208

If you always have 3 lines you could just create a String per line and a List of integer per line. Then you get a line in a String, split the string using space as delimiter. Finally you parse the integers and add them to your list.

Here the code for one line :

String line1 = sc.nextLine();
List<int> list1 = new ArrayList<int>();
String[] ints = line1.split(" ");
for(String s : ints) {
    list1.add(Integer.parseInt(s));
}

Upvotes: 0

Related Questions