How to read array of integers from the standard input in Java?

前端 未结 2 1456
心在旅途
心在旅途 2021-01-07 15:52

in one line from the standard input I have 3 types of integers: the first integer is id, the second integer is N - some number, and after that follows N integers, separeted

2条回答
  •  执念已碎
    2021-01-07 16:15

    Use Scanner and method hasNextInt()

    Scanner scanner = new Scanner(System.in);
    
    while (scanner.hasNext()) {
    
         if (scanner.hasNextInt()) {
            arr[i]=scanner.nextInt();
            i++;
         }
      }
    

提交回复
热议问题