• 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

Problem with loop and getting user input

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a simple program to convert temperature. There is no problem with the actual results - it gives correct results. The problem that I have is that it is supposed to loop until quit. Now the first iteration works just fine. If I select say 2 the second time, it does not work as it should. It seems to ignore my input for some reason. I cannot see what is wrong - can anyone help?

 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a bit too much code in your main() method for my taste, but putting that aside...

whenever the code is not doing what you think it should, especially when you are learning the language, "System.out.printnl()" is your best friend. If your code isn't following the switch branch you think it should, print out your option value just before you enter said switch.  Prove to yourself it is what you think it is, and find out if it's not.
 
Bartender
Posts: 390
47
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch! Try this: follow the code in your mind, or if you want, in a debugger. What happens after the switch statement?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Try these two Unicode escapes for temperatures: \u2103 and \u2109.
Don't create new Scanner objects. You only need one Scanner object to read System.in. It is better to wrap that in a utility class, which you can find (in incomplete form) if you search my posts for Scanner utility class. Several of us have similar utility classes, But you might not have the time for such searches just at the moment.
Change all your int literals in lines 102, 109, and similar to double literals. Change 5 to 5.0, for example.
Please check the capitalisation conventions for method names: use cToF(double) please instead of CtoF(double). celsiusToFahrenheit(double) would be better still, but in the context of a temperature class, you can get away with the short form. More information in the old Sun style guide.
I am pleased to see you have a constant int called QUIT, but it would be better to write similar constants for 1 and 2. Alternatively you can use text in switches. More information in the Java™ Tutorials, but that link doesn't cover the newer forms of switch in Java12+.
I am pleased to see you have (mostly) indented your code nicely at the left, but many of your lines are too long. Please break them into shorter lines (Sun Style Guide again):-I would prefer to see an object created:Also why are you making so many methods static? I think a cToF() method should be static, but I would still need an explanation why. Those methods are unduly long:-Something else about lengths: your loop is so long that you didn't notice (nor did I notice it at first) . . . [deleted part so as not to give too much of a direct answer]. I think that is the problem you are noticing.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I deleted part of my answer because Fred and JJ have already answered. Please follow their suggestions and see what you come up with. We think it is better to let you find the problem, and I had let the cat out of the bag a bit too early. I think the switch can be tidied up, but the problem you are seeing isn't in the switch.
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A potential problem is that you are creating a new Scanner( System.in ) more than once during the execution of your code. You may get away with this or it may hiccup. The proper way is to only do this once FOR THE ENTIRE PROJECT and for THE ENTIRE TIME IT IS RUNNING.

The easiest way to do this is by making 'keyboard' a static final constant and then using that everywhere. Note that constants are, by convention, in all upper case.  Also, never ever close KEYBOARD even if your IDE complains about it.
 
Matthew Stein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone - I have corrected the errors and it works fine now - this is the finished version:

 
Matthew Stein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:There's a bit too much code in your main() method for my taste, but putting that aside...

whenever the code is not doing what you think it should, especially when you are learning the language, "System.out.printnl()" is your best friend. If your code isn't following the switch branch you think it should, print out your option value just before you enter said switch.  Prove to yourself it is what you think it is, and find out if it's not.



Very helpful suggestion - using this I found the three hidden errors in a methodical way..
 
Matthew Stein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I deleted part of my answer because Fred and JJ have already answered. Please follow their suggestions and see what you come up with. We think it is better to let you find the problem, and I had let the cat out of the bag a bit too early. I think the switch can be tidied up, but the problem you are seeing isn't in the switch.



Thank you - all your comments are noted - but im just working through a text at the moment and have not been introduced to the things you mention. But thanks.
 
Matthew Stein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:A potential problem is that you are creating a new Scanner( System.in ) more than once during the execution of your code. You may get away with this or it may hiccup. The proper way is to only do this once FOR THE ENTIRE PROJECT and for THE ENTIRE TIME IT IS RUNNING.

The easiest way to do this is by making 'keyboard' a static final constant and then using that everywhere. Note that constants are, by convention, in all upper case.  Also, never ever close KEYBOARD even if your IDE complains about it.



Thank you - I will use this technique in future.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Stein wrote:. . . just working through a text . . .

Which text? You already know about System.out.println. Use it to print option, as Fred suggested.

You would appear to have worked out what I deleted (well done), that you are requesting input for option twice in the loop. Removing lines 48‑49 seems to have cured your trouble.
Adding absolute zero has unfortunately made your method even longer, and introduced quite a bit of repeated code.
 
Matthew Stein
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The book in using is “Java in Two Semesters” Fourth Edition, Charatan & Kans. published by Springer.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the information. That's not a book I am familiar with
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic