wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Back to basics:  if statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Back to basics:  if statement" Watch "Back to basics:  if statement" New topic
Author

Back to basics: if statement

Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
In y course notes it states the following for the if statement:

if (expression)
statement1;
else if (expression)
statement2;
else
statement3;

So I had ago at implementing this. My code is as follows:



Why then do I get the error message:

C:\java>javac BothCelsius2Fahrenheit.java
BothCelsius2Fahrenheit.java:55: 'else' without 'if'
else if (input.equalsIgnoreCase ("Celsius") )
^
1 error

Are my course notes wrong or have I NOT understood something?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Note the indentations and blocks...



"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Joyce Lee
Ranch Hand

Joined: Jul 11, 2003
Posts: 1392


Remove the semicolon, that error message will be gone.
[ November 16, 2004: Message edited by: Joyce Lee ]
Dave Cryder
Greenhorn

Joined: Aug 11, 2004
Posts: 9
You have a semi-colon at the end of your if statement, thus terminating the statement without doing anything. The subsequent "else if" is not associated with any if statement, and an error is generated.

I absolutely <i>hate</i> when I do that.
David Dickinson
Ranch Hand

Joined: Nov 11, 2004
Posts: 66
I can't run code on the computer im using now but I would imagine it could be related to the ; at the end of the first if...



Might want to remove that ;
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4442
    
    5

if (input.equalsIgnoreCase ("Fahrenheit") );


That semicolon tells the compiler there's an empty statement attached to the if. The next "{" just starts a block that's not attached to anything, which is still legal up to that point.


Junilu - [How to Ask Questions] [How to Answer Questions]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I was just going to edit my post to mention that semicolon, but I see a few people beat me to it.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hey, did anybody see that semicolon at the end of the if statement?


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11139
    
  16

You're all wrong... the problem is the semi-colon on the end of the if statement!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Maureen Charlton
Ranch Hand

Joined: Oct 04, 2004
Posts: 218
The problem sure was the semi-colon at the end of the if statement!!

Thank you!

(I spent hours looking for that!! Must of got tired.)

- Don't know what to do to close a thread??? But this can now be closed.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think I see the problem, but I'll wait and see whether anyone else notices.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Back to basics: if statement
 
Similar Threads
Methods: parameters
Why am I not getting the right answer
Which is the best practice?
java temperature converter
equalsIgnoreCase