I began this problem by creating my class and main, and declaring my variables. I figured that I needed 2 variables of class double for storing user input and the output from the calculation, and 1 variable type string for the unit the user wanted to convert (C/F). I realized shortly afterwards that i would need another variable of type string for the user to input whether they wanted to continue convering or not (Y/N).
I knew that I would need a while loop, so the user could repeat the program several times if they wanted. I began with a while the condition.equals "Y" and prompted the user to enter a temperature with an output, and an input using scanner. Then I asked for the units the same way and began with a if statement for each circumstance, celcius or fahrenheit. If the user entered a C, i made a formula for the output using the formula given, and vice versa for fahrenheit. I then printed out the conversion and prompted the user to enter whether they wanted to continue (Y/N) using scanner. I ctlr c + v this if statement and replaced the formula with the one for fahrenheit given.
After this I compiled and debugged my program a bit before giving it a try. Immediately I realized I had a few things to tweak, specifically that the program crashed if the person didnt enter exactly a capital F or C, same with the Y or N prompt. Also their was a problem if a person entered a string instead of a number in the temperature input. After some research I learned about the equalsIgnoreCase which I could use in my while and if statements so that it didn't matter whether the letter entered was capital or lowercase. I also wanted the user to be prompted to enter a f or c if they entered something else, so I added an else statement at the end of my while loop that would be triggered if the input was something different than a C or F.
The only issue I had remaining was the problem where if a person entered something other than a number for a temperature the program would crash. I played around with trying to figure out exceptions and try...catch statements but couldn't get anything to work. I also tried an if statement but it made my program not work at all so I took it out and left the bug. Maybe I will learn how to change this further on in the course.
I now have a working try...catch statement, had quite a bit of problems getting the user to go back to the prompt afterwards then learned about the continue; statement in java which brings the user back to the start of a loop. Now I have a method for catching invalid data for both input types.
The Landing is a social site for Athabasca University staff, students and invited guests. It is a space where they can share, communicate and connect with anyone or everyone.
Unless you are logged in, you will only be able to see the fraction of posts on the site that have been made public. Right now you are not logged in.
If you have an Athabasca University login ID, use your standard username and password to access this site.
We welcome comments on public posts from members of the public. Please note, however, that all comments made on public posts must be moderated by their owners before they become visible on the site. The owner of the post (and no one else) has to do that.
If you want the full range of features and you have a login ID, log in using the links at the top of the page or at https://landing.athabascau.ca/login (logins are secure and encrypted)
Posts made here are the responsibility of their owners and may not reflect the views of Athabasca University.