-2

I'm trying to take the following line as input in Java but not getting the desired output. I want all of those integers in an Array List.

Input:

  • 2 (test cases)
  • 5 (size of first test case array)
  • 1 2 3 5 4(Elements in first test case array)
  • 4 (size of second test case array)
  • 66 45 778 51(Elements in second test case array)

Code:

import java.util.*;
import java.lang.*;
public class Main
{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //System.out.println("Hello World");
        int tc = sc.nextInt();
        for(int i = 0; i < tc; i++){
            ArrayList<Integer> list = new ArrayList<Integer>();
            int t = sc.nextInt();
            String line = sc.next();
            String[] arr = line.split(" ");
            for(int j = 0; j < t; j++)   
                list.add(Integer.parseInt(arr[j]));
            System.out.print(list);
        }
    }
}

Output

1
5
1 2 3 4 5
[1]
SMR
  • 1
  • 3
  • 1
    Please [edit] your question to include your full source code you have as a [mcve], which can be compiled and tested by others. – Progman Oct 20 '19 at 15:41
  • "getting errors"—Specifically...? – khelwood Oct 20 '19 at 15:54
  • Possible duplicate of [What's the difference between next() and nextLine() methods from Scanner class?](https://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-scanner-class) – Progman Oct 20 '19 at 15:55
  • @khelwood my bad. just edited it. I mean, not getting desired output. – SMR Oct 20 '19 at 15:58

4 Answers4

0

Use sc.nextLine(). It returns a String, but by splitting the input after every whitespace you would get a String[] containing all of your integers. If you loop over them and convert them to an int, you can add them to your list.
Here's an example of how I would do it:

import java.util.ArrayList;
import java.util.Scanner;

class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("How many numbers?");
    int tc = Integer.valueOf(sc.nextLine());

    for(int i = 0; i < tc; i++) {
        System.out.println("Next number");
        ArrayList<Integer> list = new ArrayList<Integer>();
        String line = sc.nextLine();

        String[] arr = line.split("\\s");
        for(int j = 0; j < arr.length; j++){
            list.add(Integer.parseInt(arr[j]));
        }
        System.out.print(list);
    }

    sc.close();
  }

}
Yoshie2000
  • 103
  • 8
0

Im not quite sure what your question is but I think you are just interested in reading a string of numbers (1 2 3 4 5 4) and convert it to an array [1,2,3,4,5,4].

String input= "1 2 3 4 5 4";
String[] stringList = input.split(" ");
int[] integerList = new int[stringList .length()];
for(int i = 0; i < listOfS.length(); i++) {
    integerList[i] = Integer.parserInt(stringList[i]);
}

At first you have to split the String string into its elements by using input.split(" "). That will split the String input at every space symbol. Then you have to convert the String[] into an int[]. This is done by iterating over the string[] and parsing its content with Integer.parseInt() to an integer.

0

Does this do what you want.


    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    public class Main {
       public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          int tc = sc.nextInt();
          for (int i = 0; i < tc; i++) {
             List<Integer> list = new ArrayList<>();
             int nElements = sc.nextInt();
             while (nElements-- > 0) {
                list.add(sc.nextInt());
             }
             System.out.print(list);
          }
       }
    }

WJS
  • 22,083
  • 3
  • 14
  • 32
0

Here's a simple solution using Stream for numbers processing:

List<Integer> list = new ArrayList<>();

Scanner scanner = new Scanner(System.in);
int testCases = Integer.parseInt(scanner.nextLine());

for (int i = 0; i < testCases; i++) {
    scanner.nextLine(); // skip "size" line as we don't use it in this solution
    Arrays.stream(scanner.nextLine().trim().split("\\s+"))
            .map(Integer::parseInt)
            .forEach(list::add);
}

list.forEach(e -> System.out.printf("%d ", e));

Input

2
4
  2 3   4 5   
2
 99    77

Output

2 3 4 5 99 77 

Explanation

  • Arrays.stream(...) - create a stream from the elements of the supplied array
  • scanner.nextLine().trim().split("\\s+") - read a line from the Scanner, trim empty spaces at the start and the end, split by one or more empty spaces it into a String[]
  • .map(Integer::parseInt) - convert every String element to an Integer
  • .forEach(list::add) - add each Integer element to the list
MartinBG
  • 1,034
  • 8
  • 13