I tried to print out a string that includes Spanish accents on the screen. I get different answer with the same input, but with different approaches.
My first appro
using the second method, the bytes of the String are input directly into the app. Using the first method, you are relying upon the Locale of the Scanner
see http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#useLocale(java.util.Locale)
Not an answer
You still need to check some things.
The java source encoding used by the java compiler must be the same as the encoding of the text/editor.
Check the default encoding:
System.out.println(System.getProperty("file.encoding"));
Redirect the System.out/console to a file.
Try the real console, not that of the IDE.
I find the behaviour puzzling. It would look as if the Scanner took a charset encoding UTF-8 and then had to write it to US-ASCII, so umappable characters are given by �
. That cannot be. It looks like Scanner would be "buggy" - which I doubt.
Try dump the string:
for (int i = 0; i < s.length(); ) {
int cp = s.codePointAt(i);
System.out.printf(" %x", cp);
i += Character.charCount(cp);
}
System.out.println();
(Normally not done)
System.setProperty("file.encoding","Windows-1252");