user3063022
user3063022

Reputation: 25

using getText() with a double

I am trying to get the content of a JTextField and I am setting the contents to a double variable.

However I think I need an alternative as I am unable to use getText() as it is only for string variables?

numbers = webpage.getText();

returns the error incompatible types?

Upvotes: 1

Views: 10869

Answers (2)

GETah
GETah

Reputation: 21419

returns the error incompatible types?

This is because JTextField.getText() returns a string not a double.

You should convert your string into a double to be able to assign it to your numbers variable. The following should solve your problem:

try{
  numbers = Double.parseDouble(webpage.getText());
}catch(NumberFormatException ex){
 // The user entered an invalid number, report the error
}

Upvotes: 4

jgon
jgon

Reputation: 688

Just use

numbers = Double.parseDouble(webpage.getText());

that will convert the string into a double for you.

Upvotes: 9

Related Questions