my dog learned polymorphism*
The moose likes Beginning Java and the fly likes if 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 "if else statement" Watch "if else statement" New topic

if else statement

Jem Edwards

Joined: Mar 28, 2003
Posts: 22
I'm trying to learn Java in an Internet class and I'm hoping that the Saloon can be my homework buddy - will that be ok? I'm using two programs - JB8 and Textpad to work on my exercises. My book is "JAVA - An Introduction to Computer Science & Programming". Nope - I don't expect anyone to do my homework for me (need to pass that Final by myself!) but I expect this would be just like coaching or tutoring. Any help over the next few months will be greatly appreciated.
I am working on a temperature conversion exercise. I asked the user to input a temperature number then I ask the user to input a 'C' or 'F' for Celsius or Fahrenheit. My programs works on the first part when I input a number to convert to Celsius.
However after I added the next if/else statement to convert my input to Fahrenheit I get an error message. Can you please tell me why this isn't working. It's been two hours and I can't figure it out. I just copied and pasted the previous if/else statement that worked and it didn't give me an error message.
if ((fahrenheitCelsius!='C') && (fahrenheitCelsius!='F'))
System.out.println ("Enter either an uppercase \"C\" for Celsius or uppercase \"F\" for Fahrenheit:");
else if (fahrenheitCelsius=='C')
degrees = (degrees-32)*5/9;
System.out.println ("The temperature is " +degrees +" degrees Celsius");
else if (fahrenheitCelsius=='F')
degrees = (9*(degrees/5) + 32;
System.out.println ("The temperature is " +degrees +" degrees Fehrenheit");
System.out.println("Want to make another temperature conversion?");
The error I get is:
C:\Documents and Settings\EDWARDS_J\CS151\ 'else' without 'if'
else if (fahrenheitCelsius=='F')
1 error
Tool completed with exit code 1
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
The problem is that you need to add curly braces ({ }) to enclose multiple statements that are executed with a control statement (such as if or else). Change your code to the following:

Notice the curly braces which I added. Also, since you are just starting, I would like to point out some things that will help you form good habits, and hopefully in the long run, help you avoid hours of searching for simple mistakes.
First of all, you should indent a line of code after an opening curly brace, as I did with the if statements above. This also applies to classes and functions (which you'll get to eventually). Another nested curly brace block simply indents another level. Closing curly braces bring the indentation back out one level. (Usually this indentation is 2 or 4 spaces. Your editor probably takes care of it. In fact, TextPad is a great editor for coding.)
Of course, I also realize that perhaps you did have this indentation, but the web page here ignored it all. For future reference, you can use the CODE tags to preserve indentation. There is a button just below the area where you type your message that inserts these tags automagically for you.
Well, I sure hope this helps with your problem. And welcome to the exciting world of Java!

Java API Documentation
The Java Tutorial
Jem Edwards

Joined: Mar 28, 2003
Posts: 22
Thank you so much for your quick response. I will pay attention to those curly brackets. Once I put them in properly I found another problem that I fixed - missing a closing parens on one of the the conversion formulas and then I was able to correct the formulas so they worked properly in the conversion.
I am so estatic about this site. Would you believe I posted a question on something in the textbook that I didn't understand 10 days ago and no one reply - neither another web student nor even the instructor. I'll be posting my question here shortly.
Thank you again!
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15300

Jem, Welcome to the Ranch! It's always good to see new faces here.
I just wanted to congradulate you on how you presented your question. So many newcomers really do think that we are here to do your homework for you.
You presented your problem, had already made several attempts at solving it yourself, then asked for help on the relivent code you were having problems with.
And, you got some help instead of some comment about "we aren't here to do your homework".
Hopefully, future newbies will see your topic and take notes.
Hope to see you around here a lot more.

GenRocket - Experts at Building Test Data
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

Tell everyone in your class that they should post their questions here!

Please ignore post, I have no idea what I am talking about.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Mike will just do your homework for Mike, still owe you that case of shiner.
Jem Edwards

Joined: Mar 28, 2003
Posts: 22
Thanks for the warm welcome everybody. I'm pretty frustrated about the lack of web participation in my class during these past two weeks.
I put up a discussion about how to do equalsIgnoreCase early this morning and no one has responded (as usual the books sucks). If I haven't heard anything by tonight I'll be posting again.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

Mike will just do your homework for Mike, still owe you that case of shiner.

A consequence of raising five lazy kids Why don't you drive up to Longview with the shiner and I'll throw some ribeyes on the grill?

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Well I will not be doing a lazy s*d's homework for it.
Show you're willing to make a try and you get help, but throwing up your hands and effectively saying "do it for me" isn't going to get you nowhere.
Now a tip I learnt a long time ago:
-40.0 degrees F is -40.0 degrees C exactly.
So take a look at this:

The above gives a lead to a method

[ March 28, 2003: Message edited by: Barry Gaunt ]
[ March 28, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Consider Paul's rocket mass heater.
subject: if else statement