0

How can I .getText(long) ????? I can not figure out how to get a string from a GUI and turn a String into a long .

3
Contributors
3
Replies
4
Views
10 Years
Discussion Span
Last Post by ceyesuma
0

As far as i know getText doesnt have any parameters. you would just say

String x=box.getText();
Long l= Long.parseLong(s);
0

As far as i know getText doesnt have any parameters. you would just say

String x=box.getText();
Long l= Long.parseLong(s);

Well, yes, mostly correct, except you want "long" not "Long" and the parameter should be "x" instead of "s". It also needs to deal with the potential NumberFormatException

JTextField txtField = new JTextField();
String textValue = txtField.getText();
long value = 0;
try {
    value = Long.parseLong(textValue);
} catch (NumberFormatException nfe){
    // you can either leave it zero or tell the user it's not a good value
    // or whatever you need to do if the text can't be parsed to long.
    System.out.println("bad value");
}
System.out.println("value="+value);
0

As far as i know getText doesnt have any parameters. you would just say

String x=box.getText();
Long l= Long.parseLong(s);

thanks

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.