If conditional woes
Hello, I am new to Java and have this code:
if (high == true)
{
System.out.println("Your donor rating is high");
else if (medium == true)
System.out.println("Your donor rating is medium");
else if (none == true)
System.out.println("Your donor rating is none");
else
System.out.println("Error, please input a correct rating");
}
I have three Boolean variables, and have initialized them correctly, however I am getting syntax errors with this conditional. Can someone explain why?
[556 byte] By [
Dark Rain] at [2007-11-11 6:49:12]

# 1 Re: If conditional woes
I think you have a problem with it because of the curly braces, you should put the else if statements outside of the first if else statement.
Something like this
if(bolean statement)
{
Code to execute
}
else if(boolean statement)
Code to execute
else if(boolean statement)
Code to execute
else
Default code to execute
If you are checking boolean variables for true/false you can just put the variable in the if statement.
if(booleanVariable)
{
Code to execute
}
I hope this helps
# 2 Re: If conditional woes
>I am getting syntax errors
Also, in the future please copy the full text of the error message to your post. That will help us with solving the problem.
Always using curly braces in if else combinations will prevent future problems. Leaving them off often causes mismatching problems between the if and else clauses.
Norm at 2007-11-11 22:41:08 >

# 3 Re: If conditional woes
For this code, could you not omit the curly braces completely? Each condition only performs one statement. I though the curly braces were only needed if a particular 'if' or 'if else' condition had more then one line of code to execute.
# 4 Re: If conditional woes
With my original code I get these two syntax errors:
Syntax error, insert "}" to complete statement (happens on the semi-colon in the statement System.out.println("Your donor rating is high");)
Syntax error on token "}" delete this token (happens on the closing brace for the if conditional)
If I change the code to this (as Wizard1988 stated):
if (high)
{
System.out.println("Your donor rating is high");
}
else if (medium)
System.out.println("Your donor rating is medium");
else if (none)
System.out.println("Your donor rating is none");
else
System.out.println("Error, please input a correct donor rating");
I get the following syntax errors on all of my Boolean variables:
Type mismatch: cannot convert from Boolean to boolean
# 5 Re: If conditional woes
Your problem is that you declared your variables as Boolean, and it is not the same as boolean
try this
class test
{
public static void main(String args[])
{
boolean high = true;
boolean medium = false;
boolean none = false;
if (high)
{
System.out.println("Your donor rating is high");
}
else if (medium)
System.out.println("Your donor rating is medium");
else if (none)
System.out.println("Your donor rating is none");
else
System.out.println("Error, please input a correct donor rating");
}
}
You can read this to find out more about Booleans http://java.sun.com/j2se/1.3/docs/api/java/lang/Boolean.html
# 6 Re: If conditional woes
Thanks for the feedback on the booleans. Although I have another problem for you guys. I removed my booleans temporarily (I'm not sure I need them), and I have this code for my donor class:
public class Donor
{
// Declare string variables
private String name;
private String rating;
// Get the donor's name and rating through a constructor
public Donor(String inputName, String inputRating)
{
name = inputName;
rating = inputRating;
}
public String returnName()
{
return name;
}
// Validate user input
public String validateDonor(String inputRating)
{
if (rating == "high")
{
return "Your donor rating is high";
}
else if (rating == "medium")
{
return "Your donor rating is medium";
}
else if (rating == "none")
{
return "You donor rating is none";
}
else
{
return "Error, please input a correct donor rating";
}
}
}
The inputRating argument is supposed to be taken from the user and fed into the class by way of the buffered reader and the String variable InputRating. The thing is, rating never gets set to anything..therefore the conditional is always false (even though I set rating to equal inputRating). Why is this happening? If you need me to post my other class I can, just let me know.
Darn semantic errors =D.
# 7 Re: If conditional woes
You should try using rating.equals() instead of the == operator
When you use the == operator, you are actually comparing two object references, to see if they point to the same object. You cannot compare, for example, two strings for equality, using the == operator. You must instead use the .equals method, which is a method inherited by all classes from java.lang.Object.
Source (http://www.javacoffeebreak.com/articles/toptenerrors.html)
I Hope this helps