5

My problem ist about a little java program written using NetBeans 7.4. There is obviously an encoding issue since I need to handle German input containing special characters (äüöÄÜÖß).

Reading in text from files works like a charm, special characters are saved and displayed as expected:

String fileText = new Scanner(file, "UTF-8" ).useDelimiter("\\A").next();

However I also need to read the user input from console - in this case I only care about the one in NetBeas itself since this code will not be used outside the IDE. Entering special characters here leads to the usual symbols (box, question mark) instead of the umlauts.

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

Input: könig
Output: k�nig

I have been stuck on this for quite a while now, having tried every option Google brought my way, but so far no luck. Most people seem to have fixed this by changing the standard encoding (Project Properties -> Sources -> Encoding), which is already set to UTF-8 though.

There is no issue using those characters in any other way, such as saving them in strings or printing them to the console. So the issue seems to be with the NetBeans console encoding setting.

I tried manually changing that without any luck. I'm not sure this setting even affects the NetBeans console, since trying to access the console object just returns null.

System.setProperty("console.encoding", "UTF-8");

Anybody have an idea where to look next? I have already exhausted all Google searches (not much useful on pages > 5, as always).

Thanks!

asto
  • 197
  • 2
  • 17
  • 1
    Have you tried this answer http://stackoverflow.com/a/2415659/1407656 ? – toniedzwiedz May 17 '14 at 09:07
  • The command line argument didn't change anything. Not sure how the second part of that answer applies to my issue, since it refers to System.out. Ist it also possible to replace System.in with a new stream set to utf-8? If so, how would the code look? – asto May 17 '14 at 11:15
  • This one worked for me: http://stackoverflow.com/questions/23726899/change-console-input-encoding-in-netbeans-8-0 best wishes. – tomasz_kusmierczyk May 19 '15 at 08:08

1 Answers1

2

I have also been confused by I/O encoding in the Netbeans console window for years, and have finally found out why.

At least on my system (Netbeans 8.1 on Windows 10), the Netbeans console confusingly uses UTF-8 for output (that's why your output works for UTF-8 input files), but uses Windows-1252 for input. (So much for POLA :)

So if you change your scanner to use that encoding

Scanner scanner = new Scanner(System.in, "Windows-1252");

everything should work fine. Or you can tell Netbeans to use UTF-8 as console input encoding by adding

-J-Dfile.encoding=UTF-8

to the variable netbeans_default_options in etc/netbeans.conf (in Netbeans installation directory).

For maximum consistency with running the app from the system command line, I would have preferred to use Windows-1252 (or rather IBM850) as Netbeans console encoding on Windows. But Netbeans seems to ignore the given switch for the console output, it always uses UTF-8, so that is the best we can do.

I really like Netbeans, but I'd wish they would clean up this mess...

Franz D.
  • 1,061
  • 10
  • 23
  • Thanks for your input. I have since moved on to differend languages and tools, but hopefully it will help other people. If anybody can confirm it I'll gladly accept it as the answer. – asto Nov 07 '18 at 23:51