Meaningless Drivel is fun!
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 Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Back to basics:  if statement" Watch "Back to basics:  if statement" New topic

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)
else if (expression)

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

Why then do I get the error message:

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

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

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

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

Joined: Feb 26, 2001
Posts: 6294

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

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

Joined: Oct 02, 2003
Posts: 11861

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

Joined: Aug 31, 2004
Posts: 11343

I think I see the problem, but I'll wait and see whether anyone else notices.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Back to basics: if statement
It's not a secret anymore!