• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Temperature Conversion using Loop

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am unable to have user input after the output of "Yes to retry. Q to quit". I think my sequence is wrong.

Output came out print out but did not allow user to enter " Yes or Q". Please advise.

See code below.



 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two things that i can say by looking at your code is that

1) You have used two if- statement (one for Celsius and other for Fahrenheit)and then an else to print the wrong entry. This will cause the else statement to execute when the user selects Celsius. So to avoid this use if - else if -else ladder i.e.



2) Before taking the input from user about continuing or quitting the program insert a nextLine() i.e. do this



This is because after you press 'C' or 'F' you press Enter key (end-of-line) which goes as an input to "ans" variable. Therefore that extra nextLine() above "ans" variable will consume the end-of-line.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The String classes equalsIgnoresCase("C") method does exactly what it says ie it will return true if the user enters "C" or "c", you don't need to test for both conditions.

Rameshwar Soni wrote:This is because after you press 'C' or 'F' you press Enter key (end-of-line) which goes as an input to "ans" variable. Therefore that extra nextLine() above "ans" variable will consume the end-of-line.


True but I suggest you put the call to nextLine() right after the call to nextDouble() so the scanner is left in usable state. This way, if in the future, you change the code to insert an extra input the scanner can be used again without having to worry about whether you first need to get rid of the newline and then whether or not you now have to remove the other nextLine() call.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Tod wrote:Please advise.


Well, I think Rameshwar's covered the main reason for your problem (which is one of the reasons why I don't like Scanner very much).

However, here are some other points that are probably worth thinking about:
1. You don't need to allocate a new Scanner every time you enter the loop.

2. You're trying to crowd all your code into main(), which is not good practise, even for a non-Object-oriented language. Think about what your program is supposed to do, and see if you can think of some methods that might help you to do it.

3. Java is an Object-oriented language, so it tends to work best with objects. What you've written is a procedure; and you could have written it just as easily using C or Basic. Tip: what about a Converter (or TempConverter) class?

4. Try to separate out all that "question and answer" (ie, Scanner/println()) code, because it basically has nothing to do with the problem you're trying to solve. Indeed, before you've finished your course I suspect you'll write "Do you want to continue?" logic at least 20 times - so how do you think you might solve that?

Just FYI, there is another, lesser-known, conversion formula which I personally find much better; especially for programs:
F = ((C + 40) * (9 / 5)) - 40
C = ((F + 40) * (5 / 9)) - 40
I'll leave you to work out why.

Winston

PS: Please DontWriteLongLines. I've tried to break them up as best I can, but it makes your Thread very hard to read.
@Rameshwar: and if you see long lines, please don't quote them. I've shortened yours too.
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The formula was given in the book, i had to use it. I will try to add the line & change my sequence. Let you know how it turns out. Thanks a bunch guys!
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works! But now i have to try to loop it so that i do not have to enter the Temperature again. Also i read the question wrongly. Supposed to enter ANY KEY to retry.
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Now i need to change when loop back, we have to skip the temeperature entry. Also any key to loop back.
 
Rameshwar Soni
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Tod wrote:

Now i need to change when loop back, we have to skip the temeperature entry. Also any key to loop back.



1) You mean to say that the program should continue for all keys except "Q" which is for Quit. Then you need to change the condition in your while loop and the condition which you have used in your newly updated code is incorrect. You can do something like this :



2) I think you need to read again what Tony Docherty said in his post i.e. when you are using the equalsIgnoreCase() method then there is no need to check for both lowercase and uppercase alphabet. Secondly no need to create the Scanner Object again and again in do-while loop and finally use the nextLine() right after taking input in "degrees" variable.

3) Finally read what Winston Gutkowski said about good practices like the length of your main() method etc.

4) I am saying this again because I think you should have implemented them in your newly updated code. Anyways don't worry much since i think you are very new to Java and probably you would learn everything with time.
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It worked! Thank you all for your kind patience. Yes, i am extremely new to programming including Java. Still trying very hard to pick up the pieces.
I actually moved the do { to below the temperature part so it doesn't need to reenter the temperature and it worked.
 
Sarah Tod
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad. How do i loop when C or F entry is invalid & loops to prompt user to enter C of F again, omitting re-entry of Temperature? Please advise. Thanks!
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you read my earlier post about equalsIngnoreCase()?

How do i loop when C or F entry is invalid & loops to prompt user to enter C of F again, omitting re-entry of Temperature?


Just put a while loop around that question and input so that while the user hasn't entered C or F you repeat the question and input the answer.

BTW your main while loop is wrong. It will only repeat if the user just presses the Enter key. You need to check for the user not entering Q as Rameshwar Soni suggested.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sarah Tod wrote:My bad. How do i loop when C or F entry is invalid & loops to prompt user to enter C of F again, omitting re-entry of Temperature? Please advise. Thanks!


This is one of those cases where you really want to put things in methods, because otherwise your main() is going to get ridiculously large. Here is a possible implementation (and it's only one of many):and then in your main() you simply call:
degrees = input(kb, "Enter C to convert to Celcius or F to Fahrenheit:", "C", "F");

I'm not usually in the habit of giving out code, but in this case, it's to show you how important it is to create lots of methods.

You should now look at ALL your code and see if you can't write other ones (or maybe even a class) to get as much code out of main() as you possibly can. Experienced programmers rarely write more than a few lines in main().

HIH

Winston
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don’t put so many blank lines in your code; it makes it harder to read.

You do not loop when you get a wrong input. At least not in that method you don’t. You ought not to convert ℃ to ℉ and vice versa in that main method, either. The main method is for starting your app off. You should have CtoF and FtoC methods. They probably count as 1368 in the most dubious classification of methods known, so they might well be static. You are also making your methods do two things, viz calculate the conversion and print it. Make your method do one thing: calculate it. Display it in a different method.
Search for “utility class” in my posts, and you should find examples of utility classes which will enable you to read your numbers from the keyboard. You will also find how you can make a method which will guarantee to get a double from a Scanner and prompt for new entries if the entry is invalid. That is where you have a loop, to ensure your method returns a valid number. You should be able to enhance that to insist on a value ≥‑273.15℃ or ≥‑459.67℉ (=absolute zero). Once you have created that utility class, you can reuse it in your next app.
 
I am going to test your electrical conductivity with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic