-3

I have a table in my localhost where I have initally set the Rating(//Column name) value to 0 to all the items.I want to run a loop.But inspite of updating in column it runs else loop.

  if(String.valueOf(StrRating).equals("0")){

                params.put("ratingPoint",String.valueOf(average));
           //if condition doesn't work.
            }
            else {
                float total2 = 0;
                total2 += StrRating;
                total2 += average;
                float finalAverage = total2 / 2;
                Toast.makeText(Survey.this,String.valueOf(StrRating),Toast.LENGTH_SHORT).show();
                params.put("ratingPoint",String.valueOf(finalAverage));
            }
  • Is `StrRating` a number? – Murat Karagöz Jul 27 '15 at 13:39
  • 1
    Is there a particular reason you chose to store the rating as a `string`? It seems to be hindrance in your code since you constantly need to use `String.valueOf(StrRating)` to make any changes. Storing it as a number could simplify this greatly, and only converting to string when you are printing or displaying it somewhere. – Alexei Darmin Jul 27 '15 at 13:47
  • @MuratK. its a float –  Jul 27 '15 at 14:28

1 Answers1

4

0 is a number. You are comparing a String to a number. This will never be true, as Java will not do any implicit conversions here, just notice that one is a String, the other a number, thus never equal. Did you mean...

if(String.valueOf(StrRating).equals("0")){ // compare StrRating to String "0"

or perhaps...

if(Integer.valueOf(StrRating).equals(0)){ // parse StrRating into integer, then compare to int

?

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • I have edited.It doesn't run if (String.valueOf(StrRating).equals("0")){ –  Jul 27 '15 at 13:44
  • 1
    As we have no clue what type "StrRating" actually is, we have no idea what the problem might be. Have you tried putting a System.out.println( StrRating ); to find out what the object actually looks like? Alternatively you can show us what it is... – Florian Schaetz Jul 27 '15 at 13:46
  • I'm confused here.I've declared StrRating as float –  Jul 27 '15 at 13:54
  • 1
    `String.valueOf( 0.0f );` will product "0.0", which is not equal to "0". What you want to do is comparing floating point numbers, which is tricky, since they aren't exact values. Your StrRating could be ALMOST 0.0, but not quite, for example. – Florian Schaetz Jul 27 '15 at 14:00
  • So, to add to that, I would first make sure that you really need a float there, and if you really want to compare it to 0.0, google "Java float compare", because that's tricky topic that can't be easily explained in a short posting here. – Florian Schaetz Jul 27 '15 at 14:05