Hello all,
In a java program I'm trying to display the degree symbol in the command prompt using println(), but it's not displaying properly. Here is the statement:
System.out.println( "\u00b0" );
Thanks for any suggestions.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hello all,
In a java program I'm trying to display the degree symbol in the command prompt using println(), but it's not displaying properly. Here is the statement:
System.out.println( "\u00b0" );
Thanks for any suggestions.
A couple of questions:
- Is it displaying anything? Anything at all?
- Are you running from an Operating System terminal command line? Or from a "command line" inside an IDE like Netbeans or Eclipse?
- What happens if you print ("\260")? That is the single octal byte corresponding to hex 0xb0.
From my Linux system operating from a gnome-terminal command line, both show me a "degree" symbol in the output.
I'm not sure how it shows up on browsers around the world, but...
Output, pasted from my gnome terminal:
Here it is: ° There it was. Here it is again: ° There it was again.
When I redirected the output to a file, here's the hex dump of the file:
00000000 48 65 72 65 20 69 74 20 69 73 3a 0a c2 b0 0a 54 |Here it is:....T| 00000010 68 65 72 65 20 69 74 20 77 61 73 2e 0a 48 65 72 |here it was..Her| 00000020 65 20 69 74 20 69 73 20 61 67 61 69 6e 3a 0a c2 |e it is again:..| 00000030 b0 0a 54 68 65 72 65 20 69 74 20 77 61 73 20 61 |..There it was a| 00000040 67 61 69 6e 2e 0a |gain..| 00000046
Both lines result in hex bytes 0xc2, 0xb0 being deposited in the file. Note that I can also print the "degree" symbol by executing the following from a command line:
echo -e "\0302\0260"
That puts out the characters 0xc20xb0
Bottom line: Anything I can print from a command line on my system can be done with println() from Java. (java 1.6.0_22)
This particular system is "plain vanilla" Centos 5.8 Linux (directly descended from RedHat source they say) with the default US locale installation stuff.
I'm not sure whether (or how) any kind locale environment variables have anything to do with how unicode is displayed. Maybe someone from our large international community can enlighten us...
Cheers!
Z
Last edited by Zaphod_b; October 14th, 2012 at 02:41 PM.
Last edited by nicnicman; October 14th, 2012 at 02:55 PM.
Can't help directly with Windows 7, but here's the kind of thing I used to do with Windows XP:
Get out of TextPad and run the command line program from a command line. Sometimes editors are set up to use a different Code Page than what you see from a "raw" command line.
Try all characters below 256 to see if any look like what you want to see:
Scroll back through the output and see if anything looks like the symbol that you want. For me, 0xf8 (decimal 248) looks like a winner.
So, I change the program a little to create an actual output line that I want to "look good" in the "real" application.
import java.util.*; public class Z { public static void main(String [] args) { char degreeChar = '\u0000'; for (int i = 0; i < 256; i++) { System.out.printf("0x%02x (%d): %c\n", i, i, degreeChar); ++degreeChar; } // Visually selected from screen output on Windows XP degreeChar = '\u00f8'; System.out.printf("\nUsing degreeChar = (int)0x%04x: %c\n\n", (int)degreeChar, degreeChar); double degF = 75.0; double degC = (degF - 32.0) / 1.8; System.out.printf("%.2f %cF = %.2f %cC\n", degF, degreeChar, degC, degreeChar); } }
I have attached a screen shot of the last 23 (or so) lines of output from the program.
Bottom line: IWFMYMMV (It Works For Me; Your Mileage May Vary.)
Cheers!
Z
Last edited by Zaphod_b; October 14th, 2012 at 04:30 PM.
nicnicman (October 14th, 2012)
I've never really had much luck with characters outside the standard 127 ASCII encoding in Windows command prompt...
You can use the chcp command to change the character encoding, but I've had mixed results with this. The documentation also isn't great, the best place I've found for information on what number corresponds to what encoding is Wikipedia, and I suspect this may even be incorrect.
edit:
Funny enough I checked the encoding Java was using and in Eclipse it launches with utf-8, in the console it launches with windows-1252. I'll do a little more investigation to see if I can figure out what's going on here...
I also found better documentation from Microsoft.
The only really reliable solution I found was to use windows-1252 (chcp 1252). For some reason using UTF-8 encoding with chcp 65001 is giving me extra characters...
In command-prompt you can change the encoding to UTF8 using -Dfile.encoding=UTF8, or to windows-1252 using -Dfile.encoding=windows-1252.
Last edited by helloworld922; October 14th, 2012 at 06:18 PM.
jps (October 14th, 2012)
Zaphod, I went through the process you presented and came up with the same thing you did: \u00f8. Btw, it works great!
Thanks a lot.