2

In my book I read following:

import java.io.Console;
// better to print thro' Console object - it handles "special characters" better
class SpecialCharHandling {
    public static void main(String []args) {
    // string has three Scandinavian characters
        String scandString = "å, ä, and ö";
    // try printing scandinavian characters directly with println
        System.out.println("Printing scands directly with println: " + scandString);
    // now, get the Console object and print scand characters thro' that
        Console console = System.console();
        console.printf("Printing scands thro' console's printf method: " + scandString);
    }
}

Here is what this program prints: Printing scands directly with

Printing scands directly with println: •, •, and ÷ 

Printing scands thro' console's printf method: å, ä, and ö 

As you can see from this output, Console’s printf() method (and other methods) have better support for special characters.

I try to launch this application locally. I got a different result:

Printing scands directly with println: ├е, ├д, and ├╢
Printing scands thro' console's printf method: Г?, Г¤, and Г?

How to explain difference with book ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • Weird, I get `Printing scands directly with println: å, ä, and ö` and then `System.console()` returns null and I get a NPE... How are you running it? – azurefrog Jul 08 '14 at 18:14
  • 3
    @azurefrog For `null` console (unrelated to this), http://stackoverflow.com/questions/4203646/system-console-returns-null – Sotirios Delimanolis Jul 08 '14 at 18:16
  • @SotiriosDelimanolis Thank you, I was unaware of that issue in eclipse. From the command line I get `Printing scands directly with println: σ, Σ, and ÷` and `Printing scands thro' console's printf method: å, ä, and ö` so it seems to be highly dependent on exactly how you run the program. – azurefrog Jul 08 '14 at 18:18
  • @azurefrog in IDEA I see same issue(null) – gstackoverflow Jul 08 '14 at 18:54
  • Which book are you referring to? – Vince Sep 09 '14 at 18:39
  • @Vince Emigh http://4.bp.blogspot.com/-2NxUP8WCRv4/UsVlNmkNOjI/AAAAAAAAA6Q/zR-D5SeZ3ts/s1600/Oracle+Java+SE+7+Certification+OCPJP+7+exam+book.jpg – gstackoverflow Sep 10 '14 at 18:58

0 Answers0