• Post Reply Bookmark Topic Watch Topic
  • New Topic

Beginner having trouble with a temperature converter (celsius to fahrenheit) code  RSS feed

 
Simmon Thind
Greenhorn
Posts: 4
Java MS IE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! This is my first time on this site. I am a high school student learning Java (only my first week learning it) and having trouble with this code while I presume would be simple. I cannot complie it because of the errors and I tried very hard to fix them myself but I couldn't. Could any of you maybe help me? The code is supposed to give the user the choice to convert between Celsius and Fahrenheit (and vice-versa), and restart until they enter the number "99" when prompted.

 
Daryl Cofer
Greenhorn
Posts: 19
Eclipse IDE Java Ruby
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as the errors you are receiving goes, here are a few fixes:

1. The proper syntax to initialize a variable is: <type> <var-name> = new <type>(params);
That is why lines 20 and 21 are angry with you.


2. You have a catch block without a try block. These two go together. In your case, since you're just trying to prevent any and all errors from occurring(not best practice, don't get used to doing this) you should probably just wrap your whole main method inside the try/catch.

3. In your two methods at the bottom, it is complaining because it doesn't know what 'x' is. You can make this information available by passing in a value for x in the parameters or creating the variable within the method itself.


4. Your second while loop is trying to reference the variable 'input' which is out of scope.

This can be fixed by moving the Scanner initialization code up here:


5. You're not using the add and sub methods anywhere in your code so if you're going to use them, then put them to work. Otherwise they're better off just being deleted.
 
Simmon Thind
Greenhorn
Posts: 4
Java MS IE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much Daryl for helping me! This will help me a lot!
 
Simmon Thind
Greenhorn
Posts: 4
Java MS IE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daryl! My code actually worked! Thank you! I learned a lot from your post. It's so exciting when your learning java and something actually works. Here is the code (just a few things to adjust which I can do) but thank you!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should start properly indenting your code. It really helps you understand the flow better, helps you find mistakes easier, and if you want other people to read it, they will expect it to be.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simmon Thind wrote:It's so exciting when your learning java and something actually works.

Isn't it though? Well done.

Here is the code...

An observation for you: Have you noticed how the code to solve the problem (temperature conversion) is dwarfed by the amount of code you've written for the user interaction? You have precisely two lines that do the actual conversion.

Don't worry. It's normal. But now think about the number of times you might need something to calculate a temperature conversion versus the number of times you want that specific interaction code.

It's just one of the reasons we advise people to separate the problem from the input/output. If you wrote a TemperatureConverter class that ONLY does the calculation, you could store it away for later use. Furthermore, if you write it well, you could probably add Kelvin and/or Rankine to it later on if you wanted to.

Perhaps a little early yet; but it's something to keep in mind.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Also you should move 99% of that code out of the main method.
 
Simmon Thind
Greenhorn
Posts: 4
Java MS IE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the tips Campbell, Winston, and Fred. I think i'll get into the habit of writing code more efficiently so it won't effect my future coding.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simmon Thind wrote:Thanks for the tips Campbell, Winston, and Fred. I think i'll get into the habit of writing code more efficiently so it won't effect my future coding.

You're most welcome. You might also be interested in the UserInput page. Don't worry if it looks too daunting at the moment, but it might be worth a peek in a week or two.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!