Question

public String toString() {

String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + "[" + date + "]" + ":";
if (!hasFinished) {
  details += "Race not finished";
} else if  (hasFinished = true) {
  details += "\n     1st: " + competitors[0].getName();
  details += "\n     2nd: " + competitors[1].getName();
  details += "\n     3rd: " + competitors[2].getName();
} else if {
  details += "n/a"; 
}
return details;

If the position has not been set then the string literal "n/a" should be returned instead of the person’s name. -I am trying to write an IF statement if 1st,2nd and 3rd is NOT true and that n/a is returned instead.

Was it helpful?

Solution 2

You will never get details += "n/a" executed for two reasons:

  1. You're assigning hasFinished to true in the if (as other people point):

    else if  (hasFinished = true)
    

    It should be

    else if  (hasFinished)
    
  2. Most important than this, your code has this form:

    if (false) {
        ...
    } else {
        if (true) {
        } else {
            //this is false, it was supposed to be hitted before
        }
    }
    

So even if you do the first fix, the logic behind doesn't have any sense. You should use another evaluation in the second if to handle the last else statement. For example:

if (!hasFinished) {
    ...
} else {
    //the hasFinished usage here has no sense, being commented
    //just left it to show this is supported
    //if (hasFinished && someValue <= someOtherValue) {
    if (someValue <= someOtherValue) {
        ...
    } else {
        ...
    }
}

OTHER TIPS

Don't use the = operator to compare boolean values -- that's the assignment operator, so it's always true. It's already boolean, just use:

} else if (hasFinished) {

Additionally, either it's false or it's true, so there's no need for a third case. You can remove

else if {
  details += "n/a"; 
}

which was syntactically incorrect anyway.

= is used for assignment and == is used for comparison. Use == instead of = in the following statement:

else if  (hasFinished = true)

this will actually assign hasFinished value true and also the condition will be evaluated to true always. Change it to:

else if  (hasFinished == true)

for doing the comparision instead of assignment

Basically, you need to check if you actually have three finishers, and this makes up your else-if clause.

public String toString()
{
    String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + "[" + date + "]" + ":";
    if(!hasFinished) // The race has not yet finished.
    {
        details += "Race not finished";
    }
    else if(competitors.length >= 3) // Implies that hasFinished evaluated to true
    {
        details += "\n     1st: " + competitors[0].getName();
        details += "\n     2nd: " + competitors[1].getName();
        details += "\n     3rd: " + competitors[2].getName();
    }
    else // The race has finished, but there are less than three competitors
    {
        details += "n/a";
    }
    return details;
}

If you want to allow for less than three competitors, but still display 1st/2nd/3rd, you can use a ternary comparison and replace your else-if clause with an else.

public String toString()
{
    String details = number + ", " + RACE_DESCRIPTIONS[typeIndex] + "[" + date + "]" + ":";
    if(!hasFinished)
    {
        details += "Race not finished";
    }
    else // Implies that hasFinished evaluated to true
    {
        details += "\n     1st: " + (competitors.length >= 1 ? competitors[0].getName() : "n/a");
        details += "\n     2nd: " + (competitors.length >= 2 ? competitors[1].getName() : "n/a");
        details += "\n     3rd: " + (competitors.length >= 3 ? competitors[2].getName() : "n/a");
    }
    return details;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top