This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is wrong with this else statement?" Watch "What is wrong with this else statement?" New topic
Author

What is wrong with this else statement?

John Dullson
Greenhorn

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: 629

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: 3011
    
  10
phantom swordsmen wrote:
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

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


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


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

Joined: Nov 24, 2005
Posts: 14687
    
  16

"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
http://goodtoknowit.blogspot.com/
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3456
    
  12
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


Joanne
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
Bartender

Joined: Oct 02, 2003
Posts: 11246
    
  16

John,

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 ninety_nine_bottle.java
ninety_nine_bottle.java:26: 'else' without 'if'
else

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is wrong with this else statement?