How to ignore the translation of lines and spaces when typing? The input stream contains a set of integers separated from each other by an arbitrary number of spaces and line breaks. It is necessary to put these numbers in the list. When entering numbers are read only before the newline.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String numbers = reader.readLine(); Scanner scanner = new Scanner(numbers); ArrayList<Double> num = new ArrayList<>(); while (scanner.hasNext()){ if(scanner.hasNext()) num.add(scanner.nextDouble()); else scanner.nextLine(); } 
  • And what do you mean by игнорировать перевод строк и пробелов при вводе ? - post_zeew
  • And what's the problem? And your code would not interfere. - post_zeew
  • In this example, the readLine method works exactly like this (data stops reading when the line is translated). - java1cprog

1 answer 1

Solution

You can read the input data in a loop:

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main1 { public static void main(String[] args) { final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String numbers; Scanner scanner; List<Double> num; while(null != (numbers = reader.readLine())){ scanner = new Scanner(numbers); num = new ArrayList<Double>(); while (scanner.hasNextDouble()) { num.add(scanner.nextDouble()); } } //TODO: process num } catch (IOException e) { e.printStackTrace(); } } }