Using an input string in a textbox
I am trying to have the user input a number, and then that number is used to populate a text field on a jform. However it keeps giving me errors. If I have the textfield call the str it gives me a numberformatexception, if I have it call the int variable it says it has to be a string...
public static String prePaidstr = "";
public stati开发者_StackOverflowc double prePaidint = 0;
prePaidstr =
JOptionPane.showInputDialog("Enter any amount prepaid:");
prePaidint = Double.parseDouble(prePaidstr);
jTextField13.setText(InvoiceSelectionUI.prePaidstr)
parseDouble
converts a String
into a Double
, which is why it complains if you try to pass it a double
.
A NumberFormatException is thrown when parseDouble
is unable to successfully turn a String into a double; in this case it's because you're trying to parseDouble
on an empty string. prePaidStr
needs to contain something like "1.99"
- e.g. something that, to a human, looks like a Double
.
nm - I fixed it, just had one of the variables switched around
精彩评论