3

I'm having a queer issue. I have this code in Java:

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter word: ");
String word = keyboard.nextLine();
System.out.println(word);

However, for special characters, the scanner class seems to be going wrong. For example, if I type in ħabel I get ħabel printed. Eclipse's console output is set as UTF-8, sure of that, so I think it's coming from the input. I haven't found any encoding options in the Scanner class really and funnily googling about didn't give solutions neither. How could this be solved?

Thanks!

Krt_Malta
  • 9,265
  • 18
  • 53
  • 91

2 Answers2

2

When you set up a Scanner on a bare InputStream, it reads using the default charset (which for you seems to be ASCII). If you want to specify the charset, do this:

Scanner keyboard = new Scanner(new InputStreamReader(
                    System.in, Charset.forName("UTF-8")));
Russell Zahniser
  • 16,188
  • 39
  • 30
  • 1
    This question might help: http://stackoverflow.com/questions/8669056/unicode-input-in-a-console-application-in-java – Russell Zahniser Feb 02 '12 at 16:02
  • As pointed out in the question above, console input in Windows doesn't seem to work on special characters. I reverted to reading the input from a file. Thanks Russel – Krt_Malta Feb 03 '12 at 07:57
1

I believe Russel answer is correct, but it seems that your input charset is not UTF-8 Try this: Scanner s= new Scanner(new InputStreamReader(System.in,Charset.defaultCharset()));

wmz
  • 3,645
  • 1
  • 14
  • 22