aspose file tools*
The moose likes Beginning Java and the fly likes Loop Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loop Problem" Watch "Loop Problem" New topic
Author

Loop Problem

Brusse James
Greenhorn

Joined: Nov 22, 2002
Posts: 7
Hello.
I am a novice Java programmer just beginning my training and I have a problem with a program I am writing (source code follows). I cannot get my loop to run, the program states that I am to get user input on wheter they can to convert from Celsius to farenheit and vice-versa, the loop should rpaet until they press "n". Any help appreciated.

Brusse James

[ January 26, 2003: Message edited by: Brusse James ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please put your code between CODE markers and indent it correctly. (See below text entry box)
What actually happens when you run it? I can't run it because I do not have the IO classes.
Paste in some actual output.

Are you sure that this codeis in the right place?

[ January 26, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
As far as I can see, your control flow (all those 'if else if' thingies) is incorrect. Your code reads:

Two more remarks:
1) the int 'choice1, choice2' variables aren't used;
2) you forget to print the result of the calculations.
kind regards
Brusse James
Greenhorn

Joined: Nov 22, 2002
Posts: 7
Sorry about not having the code markers, I'm new at this. Anyway, when the program is run the 'if' loops are skipped, allowing not input at all, the two int variables (choice1, choice2) I have since deleted they were just a brainstorm. I assumed that nesting the 'if' statements in the do..while loop was the best route, but it hasn't worked. Here is how the screen is supposed to look:
1. Convert Farenheit to Celsius
2. Convert Celsius to Farenheit
[user makes choice]
Enter number to be converted
[user input]
[output] number conversion
[output] Do you want to continue y/n?
My Program will ask for the inputs and then go directly to the y/n question without allowing the user to enter a number.
Brusse James
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
What you're showing above is quite something different from what your code is actually doing, but let's get on ... The scenario above can be decomposed into five distinct sections:
1) ask the user what s/he wants (F --> C or C --> F);
2) get temperature input value;
3) perform the wanted calculation;
4) display the results;
5) ask for continuation.
The 3rd step can be split further into:
3a) perform F --> C calculation;
3b) perform C --> F calculation.
in Java lingo, this should boil down to something like:


Now, it's up to you to fill in the methods userChoice(), userTemperature(), calcFtoC(), calcCtoF(), displayResults() and getUserContinue().
kind regards
[ January 26, 2003: Message edited by: Jos Horsmeier ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
That's better - yup it's like Jos says
-Barry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loop Problem
 
Similar Threads
Cannot Resolve Symbol (Keyboard.readInt())
comparison methods
Back to basics: if statement
equalsIgnoreCase
need help please?