Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans GUI editor doesn't support ASCII - Java

So I'm making a basic GUI with the NetBeans IDE (in Java), and I want to make a button with a √ sign in it. It didn't let me copypaste it in, so I tried using its ASCII code - char sqrt = (char) 251. Instead of the square root sign, however, it gave me "û", and I have no idea why. Can someone please explain why this is happening, as well as offer an idea as to how I should go about this?

like image 653
Bluefire Avatar asked Aug 16 '12 20:08

Bluefire


2 Answers

Java characters are Unicode, not ASCII. Unicode codepoint 251 (U+00FB) is "Latin Small Letter U with Circumflex". To make input of various Unicode characters using a character set with only the basic ASCII symbols, Java provides a way to input Unicode characters using a literal format. So, you can do this:

char sqrt = '\u221a';

since U+221A is the Unicode codepoint for the square root symbol.

This \uXXXX format can also be used in String literals:

String s = "The square root of 2 (\u221a2) is approximately 1.4142";

If you print that String, you will see

The square root of 2 (√2) is 1.4142
like image 176
joev Avatar answered Nov 03 '22 18:11

joev


Java uses Unicode, and the Unicode value for '√' is 8730. So, this should do it:

char sqrt = 8730;
like image 44
Keppil Avatar answered Nov 03 '22 16:11

Keppil