2

I am writing a code for a calculator. Everything works fine but instead of a square root symbol (√) I am getying a question mark (?). Does this mean I can't print Unicode characters on JButton or am I wrong?

Here are two pieces containing the required character.

String operatorButtonText[] = {"/", "√", "*", "%", "-", "1/X", "+", "=" };

/***************/
if(opText.equals("√"))  
{  
try  
    {double tempd=Math.sqrt(temp);  
    cl.displayLabel.setText(MyCalculator.getFormattedText(tempd));}  
        catch(ArithmeticException excp)  
                {cl.displayLabel.setText("Divide by 0.");}  
return;  
}  

Here is an image that could be helpful. I Have marked my problem in red circle.

Java Rookie
  • 432
  • 3
  • 12
  • 2
    It means the font you are using doesn't have a symbol for that character, try a different font. – Elliott Frisch Sep 09 '17 at 12:59
  • 2
    @ElliottFrisch *"try a different font"* Great idea. But there's no need to try fonts at random. See methods of `Font` starting with `canDisplay..` for helpful methods. E.G. [`Font.canDisplay(int)`](https://docs.oracle.com/javase/8/docs/api/java/awt/Font.html#canDisplay-int-) – Andrew Thompson Sep 09 '17 at 13:05
  • 1
    Here is a [code example](https://stackoverflow.com/a/18858313/418556) that uses both `canDisplay(..)` & `canDisplayUpTo(..)`. The latter is used to check that a font can display the name of the font. – Andrew Thompson Sep 09 '17 at 13:13
  • See question [Display an Unicode character on JButton](https://stackoverflow.com/questions/42333787/display-an-unicode-character-on-jbutton) – Thomas Fritsch Sep 09 '17 at 13:20
  • I am not using any font types – Java Rookie Sep 09 '17 at 13:35
  • Well that's your problem. The *default* font that you are using doesn't have glyph for the square-root code-point. – Stephen C Sep 09 '17 at 13:44
  • BTW - `catch(ArithmeticException excp) {cl.displayLabel.setText("Divide by 0.");}` rather than presume, better to.. `..setText(ecxp.getMessage())` – Andrew Thompson Sep 09 '17 at 14:00

1 Answers1

0

Try this.

String SQR = "\u221A";
Button b=new Button(SQR);
tomaszsvd
  • 148
  • 2
  • 4
  • 15