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 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.
Also you should move 99% of that code out of the main method.
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.