1

So I've been trying to print out some lines of "-" characters. Why does the following not work?:

StringBuilder horizonRule = new StringBuilder();

for(int i = 0 ; i < 12 ; i++) {
    horizonRule.append("─");
    System.out.println(horizonRule.toString());
}

The correct output is several lines like

─
──
───
────

and so on, but the incorrect output is

â??
â??â??
â??â??â??

I'm guessing the string is not being properly decoded by println or something

Stewart
  • 17,616
  • 8
  • 52
  • 80
  • 1
    it works as you mentioned ? but Eclipse IDE ask me to save as UTF-8 – Y.Kaan Yılmaz Mar 04 '16 at 19:53
  • Java uses UTF-8 as a default. What terminal or window are you using for your output? Is it a DOS window? – Stewart Mar 04 '16 at 19:56
  • Java does *not* use UTF-8 as a default. Java uses the platform's default character set as a default. – erickson Mar 04 '16 at 19:57
  • Internally, to store Strings, etc, Java uses UTF-8. Output to a terminal or window will depend upon that terminal or window (which normally means the OS / platform) – Stewart Mar 04 '16 at 19:58
  • 4
    @Stewart Internally, Java uses UTF-16, not UTF-8... – Olivier Grégoire Mar 04 '16 at 19:59
  • Blimey! Caught with my pants down! Thanks guys. http://stackoverflow.com/questions/9699071/what-is-the-javas-internal-represention-for-string-modified-utf-8-utf-16 – Stewart Mar 04 '16 at 20:02

4 Answers4

3

The string in your code is not a hyphen but a UTF8 box drawing character.

The terminal your application is printing to doesn't seem to expect any UTF8 content, so the issue is not inside your application.

Replace it with a real hyphen (-) or make sure the tool that displays the output supports UTF8.

f1sh
  • 11,489
  • 3
  • 25
  • 51
1

You say that the IDE wants to save as UTF-8. You then probably have saved it as UTF-8.

However your compiler is likely to compile in whatever encoding your system uses.

If you write your code as UTF-8, make sure to compile it with the same encoding:

javac -encoding utf8 MyClass.java
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
0

I tried your code (I literally just copy'n'paste) using BeanShell, and it worked perfectly. So there's nothing wrong with the code. It will be your environment.

stewart$ bsh
Picked up JAVA_TOOL_OPTIONS: -Djava.awt.headless=true -Dapple.awt.UIElement=true
BeanShell 2.0b4 - by Pat Niemeyer (pat@pat.net)
bsh % StringBuilder horizonRule = new StringBuilder();
bsh % for(int i=0; i<12; i++) {
horizonRule.append("─");
System.out.println(horizonRule.toString());
}
─
──
───
────
─────
──────
───────
────────
─────────
──────────
───────────
────────────
bsh %
Stewart
  • 17,616
  • 8
  • 52
  • 80
  • This isn't an answer. What's wrong with his environment? Ask questions via comments until you can deduce the answer. – erickson Mar 04 '16 at 19:58
  • You want me to put the above output in a comment? Sheesh. At least admit that my contribution is helpful, by eliminating one possible source of the problem. – Stewart Mar 04 '16 at 19:59
  • No, just the text before it would suffice. – erickson Mar 04 '16 at 20:00
  • Well, I considered I was being helpful by showing exactly what it was I tried. I'm sorry that happened to annoy you. – Stewart Mar 04 '16 at 20:03
  • No annoyance here. I'm just offering observations. You seem a bit riled though. – erickson Mar 04 '16 at 20:07
  • The OP asked: "Why does the following not work?" My answer: "It does work." You: "That's not an answer." Me: "Rolls eyes." (As for, "Ask questions via comments until you can deduce the answer" - I'm not stopping you doing this. If you can do better than me, be my guest. Rather than just picking at someone's attempt to be helpful.) – Stewart Mar 04 '16 at 20:09
0

public class myTest1 {

public static void main(String[] args) {
    StringBuilder horizonRule = new StringBuilder();
    for (int i = 0 ; i <= 13 ; i++){
        horizonRule.append('_'); 
        System.out.println(horizonRule.toString());
    }
}

}

is correct; maybe you use a different encoding ? clear env path

Rico77
  • 1
  • 1