5

I have created a Scanner that gets input from System.in so that I can get input from the console.

Scanner scanner = new Scanner(System.in, "UTF-8");

When I do

String s = scanner.next();

and then input Слово דבר in the console, the value of the string becomes ???? ???.

The console is able to display Unicode characters, but why can't I read them?

Peter Olson
  • 131,160
  • 48
  • 197
  • 239

2 Answers2

2

It's not safe to assume System.in is UTF-8 encoded. See this question for some workarounds.

Community
  • 1
  • 1
Daniel Lubarov
  • 7,674
  • 1
  • 36
  • 55
1

This is because System.in returns text in default encoding (your default encoding is evidently not UTF-8). This should work OK

Scanner sc = new Scanner(System.in);
String s = sc.next();
System.out.println(s);

And you can read your default encoding from Java

System.out.println(System.getProperty("file.encoding"));
Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266