tags:

views:

75

answers:

3

How can I read international characters from console in java?

+4  A: 

Using the java.io.Console class, just like any other character. The question is whether the console itself supports those "international characters", but that has nothing to do with Java.

Michael Borgwardt
what does that have to do with?
mkoryak
@mkoryak: `java.io.Console` provides *"Methods to access the character-based console device, if any, associated with the current Java virtual machine..."* So then you're dealing with characters, rather than the bytes you get from `System.in`. At that point, the characters are characters, and hopefully handled properly by the console in question.
T.J. Crowder
@mkoryak: it depends on the console itself, i.e. the program that displays the command line. It may also depend on OS settings. Basically, if you can type something like "echo MyDesiredCharacters > out.txt" and have the characters show up correctly in the txt file, it should work file with Java as well.
Michael Borgwardt
+2  A: 

If you can't for some reason use java.io.Console as suggested by Michael Borgwardt, you can use an InputStreamReader around System.in, since System.in is an InputStream. You tell the InputStreamReader what character set you're expecting to receive as part of construction, either via a Charset or a CharsetDecoder.

T.J. Crowder
+1  A: 

I can't resolve the problem since I'm a beginner. I'd like to use UTF-8 charset. If I type the "aákú" (or any text with non english characters) the program will hangs up!

    Scanner sc = new Scanner(System.in);
    System.out.println("Please type any text:");
    String text = sc.nextLine();
    System.out.println("");
    System.out.println(text);

The InputSream class make some result like System.in...

The solution: set the encode properties from UTF-8 to ANSI-1250 on project properties...