This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Back to basics: if statement

 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note the indentations and blocks...

 
Joyce Lee
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Remove the semicolon, that error message will be gone.
[ November 16, 2004: Message edited by: Joyce Lee ]
 
Dave Cryder
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 7765
62
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, did anybody see that semicolon at the end of the if statement?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're all wrong... the problem is the semi-colon on the end of the if statement!!!
 
Maureen Charlton
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I see the problem, but I'll wait and see whether anyone else notices.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic