How to add a text box + combo box

Svoklavok :

I'm new to coding and wondering is there any way to get these numbers and divide them by each other

The first number (250) is from a JTextField and the second number (50) is from a JComboBox. Image of what im trying to say e.g temp = 250 / 50 = 5

then to print it out Sopln("" + temp);

not sure if that makes sense but thats the best i can describe it

Lorenz :

You can store them inside a String variable and then use them however you like.

For example

String a = t1.getText();
String b = c1.getSelectedItem();

Here you should use substring(int beginIndex) for remove the % in your b String.

This method has two variants and returns a new string that is a substring of this string.

String b_edit = b.substring(0, 2); // Now your b_edit = 50

At this point we have a = 250 and b_edit = 50

Now if you try to a + b_edit the result will be 25050. Thats because both variables are strings.

To overcome this problem, you can use parseInt().

This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.

int int_a = Integer.parseInt(a);
int int_b = Integer.parseInt(b_edit);

int temp = int_a + int_b;

If you print your temp variable , the result will be 300.

System.out.println(temp);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344938&siteId=1