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.