Search code examples
javauser-input

How to properly scan standart input text data?


I am trying to scan text data from standart input. However encodig does not work properly. For example if i provide following characters:

óś

This is what i receive:

data: '??'

The code:

public static void main(String[] args) throws InterruptedException, UnknownHostException, IOException {
    Charset ENCODING = Charset.forName("UTF-8");
    Scanner in = new Scanner(System.in, ENCODING);

    for (;;) {
        String line = in.nextLine();
        System.out.println("data: '" + line + "'");
    }
}

Solution

  • The encoding of cmd or bash should be the same with the encoding in java. In what OS the java is run? If you use windows there's some restriction regarding UTF-8, details here: https://stackoverflow.com/a/59989494/2553521