2

So I am trying to take non-ASCII text from the keyboard (System.in), Chinese characters for example, and place this text into a String object. But I've been having some trouble in doing so. My first attempt used a Scanner object:

try(Scanner keyboard = new Scanner(System.in, "UTF-8"))
{
    System.out.println("Enter text to search for (case sensitive):");
    String searchKey = keyboard.nextLine();
    ...

If the user enters non-ASCII text through the keyboard, like for example 狂浪逆襲包, searchKey will be filled with garbage. The literal content of searchKey becomes "?????" (no quotes, so it's filled with the '?' character). Doing something like:

byte[] strBytes = searchKey.getBytes("UTF-8");

shows that all elements in strBytes equal to 0x3f, which is the ASCII code for '?'. I have also tried using reader streams:

try(BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in, "UTF-8")))
{
    System.out.println("Enter text to search for (case sensitive):");
    String searchKey = keyboard.readLine();
    ...

But with this I get the exact same result as when using Scanner. Byte streams don't change anything either:

try(DataInputStream keyboard = new DataInputStream(System.in))
{
    System.out.println("Enter text to search for (case sensitive):");
    String searchKey = keyboard.readLine();
    ...

I read that System.console() might help, but that returns null when running under an IDE environment such as NetBeans. What is there left to try? All I need my program to do is to accept non-ASCII text from the keyboard and store this input as a String object.

programmar
  • 592
  • 1
  • 6
  • 16
  • Try `try(Scanner keyboard = new Scanner(System.in, "UTF-16"))` – Elliott Frisch Nov 20 '15 at 03:24
  • @ElliottFrisch Using UTF-16 I am unable to get the `Scanner` to recognize I've ended the line when I hit enter, so the program just keeps on waiting forever for more input. – programmar Nov 20 '15 at 03:39

0 Answers0