Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes What is wrong with this else statement? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is wrong with this else statement?" Watch "What is wrong with this else statement?" New topic

What is wrong with this else statement?

John Dullson

Joined: Aug 11, 2010
Posts: 1
Can someone explain to me why I keep getting a syntax error on this else statement?

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 632

remove the semicolon from line 21 after if statement.

Life is easy because we write the source code.....
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
phantom swordsmen wrote:
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1283

line number 21->
if (beerNum > 0);

There's a semi-colon after if statement....

No Kaustubh No Fun, Know Kaustubh Know Fun..
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

"phantom swordsmen",
please check your Private Messages for an important administrative matter.

And, for the fourth time There's a semi-colon after if statement.

[My Blog]
All roads lead to JavaRanch
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

removing a semicolon after the if statement.....your code is working fine....

SCJP6.0,My blog Ranchers from Delhi
Sunil Kumar
Ranch Hand

Joined: Apr 24, 2007
Posts: 76
So is the problem with line number 9 i.e. remove the semicolon

Sunil Kumar
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Sunil Kumar wrote:So is the problem with line number 9 i.e. remove the semicolon

No. That won't cause a compilation error.
But as an advance notice to the OP. When you run your program and it just sits there doing nothing, the problem is the semi colon on line 9

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

no error is on line no 21 only..........check the program.......

because else should immediately follow the nearly if statement..otherwise it gives compilation eror......

if you remove line no 22,23,24,25....then your code will also compile fine....
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955


Here's a little tip to help you get better help around here. When you ask a question like "why do I get a compiler error?" it is of unimaginable help for you to provide the exact text of the compiler error. What the compiler outputs will tell you a LOT about what your problem is, but in particular the exact line where the problem is.

In your case, you say there is an issue with your if statement...and then there are TWO if statements. So now, if someone wants to help you, they have to figure out where the error is...and some people won't bother taking the time.

Your job is to make it as easy as possible for someone to help you.

If you would have provided this:

C:\slop>javac 'else' without 'if'

we would know the exact problem is on line 26, that the IF block was closed somehow, and we'd know immediatly to look at the IF on line 21 instead of the IF on line 11.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: What is wrong with this else statement?
It's not a secret anymore!