0

Im creating a basic GUI that takes the amount entered by a user and subtracts it with a pre existing amount. However it will not let me to run the one below as I get an error of String cannot be converted to double. Using the parseInt method won't work here as im using the getText Method.

String message = txtaMessage.getText();
    String transaction1 = txtfAmountTransfer.getText();
    String reciever = txtfTransferTo.getText();
    
    lblOutputTransferInfo.setText("Your Amount of "+transaction1+" has been sent to "+reciever+" .");
    lblOutputTransferMsg.setText("With a Message: "+ message);

   double balance = 5123.84;
   
   String newBalance = balance - transaction1;//this will not work but the concept I need 
   
   lblSavingsBalance.setText(newBalance);
AJ22
  • 21
  • 6
  • 1
    You need to parse the string `transaction1` as a double before you attempt to use it for calculation, then you need to convert it back to a string. For example, `String newBalance = balance - Double.parseDouble(transaction1) + "";` See here [Javadoc - Double](https://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#parseDouble-java.lang.String) – sorifiend Apr 06 '21 at 04:44
  • 2
    Does this answer your question? [Convert String to double in Java](https://stackoverflow.com/questions/5769669/convert-string-to-double-in-java) – Osama A.Rehman Apr 06 '21 at 04:47

2 Answers2

1

It won't work because here transcation1 is a string and whereas balance is double. In java, we don't have a method to subtract string from double. So, First, you need to convert transaction1 into double. I suggest you to Double newBalance = balance - Double.parseDouble(transcation1); then lblSavingsBalance.setText(newBalance.toString()); to convert newBalance double value to string

Hope it works fine...

HoRn
  • 1,458
  • 5
  • 20
  • 25
  • You can also do it in one line using `String newBalance = balance - Double.parseDouble(transaction1) + "";` note the `+""` at the end which converts the doubles back to string. – sorifiend Apr 06 '21 at 04:56
0

If getText returns String below code will work.

        String message = txtaMessage.getText();
    String transaction1 = txtfAmountTransfer.getText();

    String reciever = txtfTransferTo.getText();


    lblOutputTransferInfo.setText("Your Amount of "+transaction1+" has been sent to "+reciever+" .");

    lblOutputTransferMsg.setText("With a Message: "+ message);

    double balance = 5123.84;

    balance = balance - Double.parseDouble(transaction1);//this will not work but the concept I need

    lblSavingsBalance.setText(String.valueOf(balance));

If you are using android,

(Assuming txtaMessage, transaction1, reciever are either TextView objects or EditText objects)

txtaMessage.getText(); //This won't return a String. This will return a CharSequence.

You have to convert it to string. So the correct method is,

String message = txtaMessage.getText().toString();

Do this for all EditTexts and TextViews.

Then transaction1 shoud be converted as Double

Double.parseDouble(transaction1);

After that you can substract transtraction1 from balance and assign it to the original balance.

balance = balance - Double.parseDouble(transaction1)

The full version of android code is below.

        String message = txtaMessage.getText().toString();
    String transaction1 = txtfAmountTransfer.getText().toString();

    String reciever = txtfTransferTo.getText().toString();


    lblOutputTransferInfo.setText("Your Amount of "+transaction1+" has been sent to "+reciever+" .");

    lblOutputTransferMsg.setText("With a Message: "+ message);

    double balance = 5123.84;
    
    balance = (balance - Double.parseDouble(transaction1));//this will not work but the concept I need
        lblSavingsBalance.setText(String.valueOf(balance));

   

If the transaction1 value is not a number, An Exception will occur. So wrapping it with try/ catch is a good idea.

            try {
        balance = (balance - Double.parseDouble(transaction1));//this will not work but the concept I need
        lblSavingsBalance.setText(String.valueOf(balance));
    } catch (NumberFormatException e) {
        //Send error message like showing a toast
        e.printStackTrace();
    }
HMR
  • 146
  • 1
  • 7
  • Your first method works however every time I enter another amount it doesn't add up by the actual amount. It goes up by the difference. If transaction1 was 100 my new balance does show 5023. However when I enter a new transaction of 200 it shows 4923 which is $200 from the old balance and not the new balance. – AJ22 Apr 06 '21 at 13:11
  • Can you explain a little bit about your text inputs. Specially, txtaMessage, transaction1, reciever. I will update my answer with best guesses, But is's good to know from you. – HMR Apr 06 '21 at 15:14
  • I've updated my code. – HMR Apr 06 '21 at 15:25
  • txtaMessage and receiver don't really have much to do with what I want I basically need the amount to subtract from the new balance each time instead of going back to my first balance. If my double balance is set to 5123.84 and I make a transaction of 100. It'll show 5023.84 which is good. But then when I make another amount transaction it starts back to the balance of 5123.84 instead of 5023.84. – AJ22 Apr 07 '21 at 00:43
  • As I told before, I've changed code. Now check the new code. These are the changed lines. Instead of declaring a new variable for new balance, we can update the old balace. So the problem is solved. balance = balance - Double.parseDouble(transaction1);//this will not work but the concept I need lblSavingsBalance.setText(String.valueOf(balance)); – HMR Apr 07 '21 at 02:00