# If/Else Compiling Error Problems

• 06-30-2013, 06:55 PM
If/Else Compiling Error Problems
The problem I am working on:

Assume* that an int*variable* age has been declared* and already* given* a value*. Assume* further that the user has just been presented with the following menu:
S: hangar steak, red potatoes, asparagus
T: whole trout, long rice, brussel sprouts
B: cheddar cheeseburger, steak fries, cole slaw
Write some code that reads the String* (S or T or B) that the user types* in into a String*variable* choice that has already* been declared* and prints out a recommended accompanying drink as follows: if the value* of age is 21 or lower, the recommendation is "vegetable juice" for steak, "cranberry juice" for trout, and "soda" for the burger. Otherwise, the recommendations are "cabernet", "chardonnay", and "IPA" for steak, trout, and burger respectively. Regardless of the value* of age , your code should print "invalid menu selection" if the character* read into choice was not S or T or B.

ASSUME* the availability of a variable*, stdin , that references* a Scannerobject* associated with standard input.

And this is the coding that I have been submitting:

Code:

```choice = stdin.next(); if(choice.equals("S")) if (age <= 21) System.out.println("vegetable juice"); System.out.println("cabernet");{} else if (choice = "T"){ if (age <= 21) System.out.println("cranberry juice"); System.out.println("chardonnay");} else if (choice = "B"){ if (age <= 21) System.out.println("soda"); System.out.println("IPA");} else System.out.println("invalid menu selection");```
But the compile error it is giving me is:
CTest.java:14: error: 'else' without 'if'
else if (choice = "T"){

But I could've sworn my if statement of <= 21 was the if before the else if?
• 06-30-2013, 07:05 PM
JosAH
Re: If/Else Compiling Error Problems
If you want more than one statement 'under' an if-clause, use curly brackets, i.e. if ( ... ) { ... } if you're not so proficient yet, always use those curly brackets.

kind regards,

Jos
• 06-30-2013, 07:07 PM
superhaNds
Re: If/Else Compiling Error Problems
Code:

```else if (choice = "T") else if (choice = "B")```
With this code you assign rather than compare, use equals method again.

EDIT: Won't compile either because an if statement requires a boolean expression to evaluate, with your example you try to evaluate a string something
that cannot happen since it's not a boolean.
• 06-30-2013, 07:09 PM
DarrylBurke
Re: If/Else Compiling Error Problems
That's why you should always use braces with if/else statements (and for/while/do..while loops). The start of your code is equivalent to
Code:

```choice = stdin.next(); if(choice.equals("S")) {   if (age <= 21) {     System.out.println("vegetable juice");   }   System.out.println("cabernet");{}   else if (choice = "T"){ // <--- huh?```
That's probably not what you intended.

And what's that empty pair of braces doing at the end of line 5 (in your posted code) anyways?

db
• 06-30-2013, 07:10 PM
DarrylBurke
Re: If/Else Compiling Error Problems
And who's the slow old sod this time?
• 06-30-2013, 07:35 PM
Re: If/Else Compiling Error Problems
Ok so I fixed the bracket issue and the else/if with T issue but now its telling me that "*⇒*****The value of _stdout is incorrect." And it highlights the phrases vegetable juice, cranberry juice, and invalid menu selection, also stating that there should possibly be something after the semi colon in the lines with vegetable juice and cranberry juice.
Code:

`System.out.println("vegetable juice");`
I have tried putting brackets at the end of those statements, I have tried making an extra if line stating (if >=21) etc, I am just starting to get at a loss because I keep trying so many different things I am confusing myself.
• 07-01-2013, 12:14 AM
superhaNds
Re: If/Else Compiling Error Problems
• 07-01-2013, 09:09 AM
JosAH
Re: If/Else Compiling Error Problems
Quote:

Originally Posted by superhaNds
Code:

```else if (choice = "T") else if (choice = "B")```
With this code you assign rather than compare, use equals method again.