Reputation: 33
If I use the "+ " sign it will just concatenate the values, but I need to find the mathematical values. I need to insert the quantity of a product. How to find the sum when multiple checkboxes are selected? I have tried this which concatenates.. Also the values need to go into the database, it is working with the concatenated values..
if(chckbx1.isSelected()){
qty= chckbx1.getText();
chckbx1.setSelected(true);
}
if(chckbx1.isSelected() && chckbx5.isSelected()){
qty= chckbx1.getText() + chckbx5.getText();
chckbx5.setSelected(true);
chckbx1.setSelected(true);
}
Upvotes: 1
Views: 830
Reputation: 6077
Yes, Using the + operator on Strings concatenates them.
So, you need to convert the Strings to numbers.
Depending upon what type of numbers the Strings contain (real or integer), you may parse them to numbers in one of the following ways:
qty= ""+ ( Integer.parseInt(chckbx1.getText()) + Integer.parseInt(chckbx5.getText()) );
qty= ""+ ( Double.parseDouble(chckbx1.getText()) + Double.parseDouble(chckbx5.getText()) );
Upvotes: 0
Reputation: 9
You may parse it to Integer using Integer.parseInt()
qty= (Integer.parseInt(chckbx1.getText()) + Integer.parseInt(chckbx5.getText())) + "";
Upvotes: 0
Reputation: 1983
You need to parse it to Double using Double.parseDouble()
qty= (Double.parseDouble(chckbx1.getText()) + Double.parseDouble(chckbx5.getText())) + "";
Upvotes: 1