Search code examples
javaeclipsejform-designer

Converting String Text of a TextBox to a numeric in Java


Ok, so i have a Text Box. This Text Box has the word BLAHH written in it. The part I don't get is how I change the text in the TextBox to an Int? I have tried putting the Int into string form. I am not trying to put the string back into Int form. im trying to have the string change the text where it displays the int number...

    //---- Money ----
    String str = Integer.toString(money); 
    Money.setText(str);
    add(Money, CC.xy(21, 1));

P.s. I'm using the JFormDesigner Plugin for Eclipse!


Solution

  • If you have a string "str" you can convert it to an int using this:

    String str = Money.getText();
    try {
    int number = Integer.parseInt(str);
    }
    catch (NumberFormatException e) {
    //What to do if Money did not hold an int?
    }