# Thread: If/Else Compiling Error Problems

1. Member
Join Date
Jun 2013
Posts
17
Rep Power
0

## 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
(Yes, this menu really IS a menu!)
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:

Java 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
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?

2. ## 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

3. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## Re: If/Else Compiling Error Problems

Java 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.
Last edited by superhaNds; 07-01-2013 at 03:44 PM. Reason: Missing clarification

4. ## 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
Java 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

5. ## Re: If/Else Compiling Error Problems

And who's the slow old sod this time?

6. Member
Join Date
Jun 2013
Posts
17
Rep Power
0

## 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.
Java 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.

7. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
4

## Re: If/Else Compiling Error Problems

re-post your fixed code.

8. ## Re: If/Else Compiling Error Problems

Originally Posted by superhaNds
Java Code:
```else if (choice = "T")
else if (choice = "B")```
With this code you assign rather than compare, use equals method again.
That won't even compile; please test your code snippets before posting.

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•