• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ParseFloat problem

 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys,

Float.ParseFloat isnt working for me..

It builds fine with no errors but when I press the button thats used in the function, the app crashes. Ive narrowed the problem down to the



"resistorValue" is a string..
If i put a normal float value in here instead, it runs fine but once I try to extract the float it keeps crashing..Ive been at it a couple of hours now and just cant solve it..
Any ideas? Something simple no doubt..

 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you expect to convert that string into a float value in some way? I don't see a reasonable way of interpreting the string "resistorValue" as a float value.

On the other hand: is it possible that you have a String variable named "resistorValue" which contains a String to be interpreted as a float value? If that's the case then your code should look like this:

 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah im basically trying to get the numbers out of "resistorValue" and use them in a calculation.
I had tried

and it will build too but ends up crashing once i hit the "band4brown" button that the listener is set up for.

If i put in

it works fine..
 
Tim Moores
Bartender
Posts: 3129
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever an app crashes, an exception stacktrace is written to the logcat. That contains the actual error message, and should be the first step when debugging app crashes. Post the full stacktrace here, and we'll try to make sense of it.
 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok cool - thanks for your help..

 
Tim Moores
Bartender
Posts: 3129
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. So what value is actually being passed to the method? You can print it to the logcat via the Log class, or display it using the Toast class.
 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


"resistorValue" is a string made up of "band1" + "band2" * "band3".
This calculation is fine as I had it in a textView to check..

The way i'm doing it is, I select band1, then band2, then band3, then band4(tolerance), once i hit band4 it crashes, i dont get a chance to hit the calculate button id I select band4.

Could it be because i'm trying use the value in "resistorValue" in the "band4" button listener before the "calculate" button has been pressed.?

Ive tried selecting band1, band2, band3, calculate, then band4 but it still crashes once band4 is selected..



 
Tim Moores
Bartender
Posts: 3129
50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Toast doesn't show the actual value being parsed, so doesn't help much. And of course it needs to be done before the parsing, so you'll know which value causes the crash. No statements are executed after the app crashes.
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So here you get something like "227 Ohms" or "63209776 Ohms" or... who knows? But at any rate trying to convert a string ending with "Ohms" to a float value is bound to fail.
 
Liam O'Connor
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, i got it sorted! Thanks.
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liam O'Connor wrote:ok, i got it sorted! Thanks.


Hopefully that means that you noticed you were doing some arithmetic to get the resistance, then turning that number into a string, and then trying to turn the string back into a number again...
 
Liam O'Connor
Greenhorn
Posts: 15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ended up just making the value a float and losing the "ohms", putting it in as a textView in the 2nd activity.

Having problems now passing the float, try'd lots of stuff but just cant get the actual value to pass.
I posted the problem as another question in this forum.

Its bloody hard work trying to figure all this stuff out, I can see why lots of people dont make it in the end - I'm trying to remain positive tho, and i appreciate all the help!
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic