Reputation: 876
Im trying to get a String from a EditText and parse to a double. But I'm getting a error.
09-26 10:42:41.589: E/AndroidRuntime(17901): FATAL EXCEPTION: main
09-26 10:42:41.589: E/AndroidRuntime(17901): java.lang.NumberFormatException:
09-26 10:42:41.589: E/AndroidRuntime(17901): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
09-26 10:42:41.589: E/AndroidRuntime(17901): at java.lang.Double.parseDouble(Double.java:318)
09-26 10:42:41.589: E/AndroidRuntime(17901): at br.com.going2.Checkincomercial.NovoCheckinActivity$4.onClick(NovoCheckinActivity.java:219)
code
String controle = "";
controle = etCustoNovoCheckin.getText().toString();
if( controle == null || controle == ""){
custo = 0.0;
} else {
custo = Double.parseDouble(controle);
}
Upvotes: 0
Views: 507
Reputation: 77904
Try to change your if
statement with:
if( controle == null || controle.trim() == "" || !controle.matches("\d+\.\d+|\d+")).
something like:
String controle = "";
controle = etCustoNovoCheckin.getText().toString();
if( controle == null || controle.trim() == "" || !controle.matches("\d+\.\d+|\d+")){
custo = 0.0;
}
else {
custo = Double.parseDouble(controle);
}
Upvotes: 0
Reputation: 627
If your EditText-Box should only support numbers, set a EditText-Box attribute.
android:inputType="number"
And you don't get into this "trouble"
Upvotes: 0
Reputation: 876
I figure it out:
if( controle.equals(null) || controle.equals("")){
custo = 0.0;
} else {
custo = Double.parseDouble(controle);
}
The error was that I was not using .equals to compare the String.
Thanks for the answers.
Upvotes: 1
Reputation: 676
try this..
String controle = "";
controle = etCustoNovoCheckin.getText().toString();
if(controle.trim().length()>0){
custo = Double.parseDouble(controle);
}
else {
custo = 0.0;
}
Upvotes: 0