Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if else statement

 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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\test.java:38: 'else' without 'if'
else if (fahrenheitCelsius=='F')
^
1 error
Tool completed with exit code 1
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
Layne
 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell everyone in your class that they should post their questions here!
 
Gabriel White
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike will just do your homework for you...lol...j/k Mike, still owe you that case of shiner.
 
Jem Edwards
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mike will just do your homework for you...lol...j/k 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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic