0

User want to raise temperature from EditText "set_temp". Currtemp is global string means current temperature

float temp = Float.parseFloat(set_temp.getText().toString());
temp-=0.1;
currTemp=new DecimalFormat("#0.0").format(temp);
set_temp.setText(currTemp);

After this i have an error in logcat

FATAL EXCEPTION: main
Process: ru.unn.caminlab, PID: 7835
java.lang.NumberFormatException: Invalid float: "49,9"

// but why?

Hryasch
  • 11
  • 2
  • 1
    `,` comma instead of `.` fullstop maybe – IAmGroot May 03 '16 at 18:37
  • 1
    the sad irony is that the answer is just one line above his question :( – svarog May 03 '16 at 18:39
  • @svarog I assume its coming from user input, as nothing else would produce a comma. Reading logs is a useful skill to start with. http://stackoverflow.com/questions/6065258/how-to-interpret-logcat – IAmGroot May 03 '16 at 18:44
  • `set_temp.getText().toString()` is the culprit, he should at least catch the exception and move on – svarog May 03 '16 at 18:46

2 Answers2

2

It looks like you are parsing a text field, that contains an invalid decimal format.

 float temp = Float.parseFloat(set_temp.getText().toString());

your set_temp field contains "49,9" when it should be "49.9"

I assume they can type into the field. You need to restrict it to a number only field. Not plain text. (to rid , input)

Add this to your xml for the field:

android:inputType="numberDecimal"
IAmGroot
  • 13,760
  • 18
  • 84
  • 154
  • android:inputType="numberDecimal" doesn't solve the problem. DecimalFormat is (0.0) but after i have 0,0 in my field... – Hryasch May 03 '16 at 19:06
0

1) Check if user having US English language locale, in many cases, user may include float literals language specific.

2) Remove set_temp.getText().toString() from Float.parseFloat() and check set_temp.getText().toString() is returning correct number string literal and then pass it to parseInt().