1

I am trying to make a gui calculator with java. But my program can't do hexadecimal calculations. This part takes the user's input:

if(event.getSource() == additionButton)
    {
        if(display.getText() != null)
        {
            temp1 = Double.parseDouble(String.valueOf(display.getText() ) );

            addOp = 1;
            clear = 1;
            display.setText(display.getText() + "+");
        }
    }

And this part does the calculations:

if(event.getSource() == equalsButton)
    {
        temp2 = Double.parseDouble(String.valueOf(display.getText() ) );
        display.setText(display.getText());

        if(addOp == 1)
        {
            if(base == 16)
            {   
                int t1Int = (int) temp1;
                int t2Int = (int) temp2;

                String temp1String = Integer.toString(t1Int);
                String temp2String = Integer.toString(t2Int);

                int temp1Int = Integer.parseInt(temp1String, 16);
                int temp2Int = Integer.parseInt(temp2String, 16);

                int Answer = temp1Int + temp2Int;
                String Result = Integer.toHexString(Answer);

                display.setText(String.valueOf(Result));
                addOp = 0;
            }

And I got an error like this:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "100a"
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at calculator.CalculatorFrame.actionPerformed(CalculatorFrame.java:528)

I know it is because of " temp1 = Double.parseDouble(String.valueOf(display.getText() ) ); ". It can't read the inputs with letters( like 100a, 65bc). How can I solve this problem? And in the ==equalsButton part how can I change the input with letters to decimal? Everything is okay without letters.

vontarro
  • 339
  • 1
  • 3
  • 14
  • 1
    There are several answers already on SO for this http://stackoverflow.com/questions/1071904/how-to-convert-hex-string-to-float-in-java http://stackoverflow.com/questions/5433807/double-to-hex-string-and-back – Kyle Falconer Nov 22 '13 at 04:24
  • Why would you be parsing a hex value as a double? Use `Integer.parseInt(String.valueOf(display.getText()), 16)`. – Ted Hopp Nov 22 '13 at 04:24

0 Answers0