Suppose I want to read from the BufferedReader in which the System.in console is fed

 Charset charset = Charset.forName("UTF-8"); Reader reader = new InputStreamReader(System.in, charset); BufferedReader bufferedReader = new BufferedReader(reader); 

1) how to correctly mark the end of reading from the console, so that the code goes further (for example, I enter "close", or press enter)

2) from reading you need to get a list of words that consist only of words and numbers

input:

 Мама ΠΌΡ‹Π»Π°-ΠΌΡ‹Π»Π°-ΠΌΡ‹Π»Π° Ρ€Π°ΠΌΡƒ! 

output

 [ΠΌΠ°ΠΌΠ° ΠΌΡ‹Π»Π° ΠΌΡ‹Π»Π° ΠΌΡ‹Π»Π° Ρ€Π°ΠΌΡƒ] 

    1 answer 1

    1) usually just have to wait for the end of the stream. In Unix, this is done using Ctrl + D , in DOS / Windows - Ctrl + Z. In addition, if you file a file at the program entrance, the end of the stream will occur when the file is over.

    2) You can use regular expression and split . Here is an example of a complete implementation using Java 8:

     import java.io.BufferedReader; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.regex.Pattern; import java.util.stream.Collectors; public class ReadWords { public static void main(String[] args) { List<String> words = new BufferedReader( new InputStreamReader(System.in, StandardCharsets.UTF_8)).lines() .flatMap(Pattern.compile("[^\\p{L}\\p{Digit}]+")::splitAsStream) .filter(s -> !s.isEmpty()) .collect(Collectors.toList()); System.out.println(words); } } 

    The regular expression "[^\\p{L}\\p{Digit}]+" means "everything that is not numbers and letters." We use this as a separator, thus getting everything else. Filtering is needed to remove empty elements at the beginning of lines if the line does not begin with a letter / number.

    • Something in the console displays an empty sheet "[]", even if the encoding is removed. Through the scanner without regulars and streams, too, does not output anything, somewhere the encoding settings got lost apparently. In Eclipse File - properties costs Cp1251 (Dafault ingerited from container), Os Windows - ketchyn
    • @ketchyn, did not think about Russian letters, \W only English matches. Fixed the regular season. Well, yes, most likely Cp1251 is necessary. - Tagir Valeev