This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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

loop problem

John Saito

Joined: Nov 08, 2005
Posts: 5
hello, i was wondering if someone could tell me what im doing wrong. i need to add a loop to my program, simply asking the user if her or she wants to continue. no matter what i try i get nothing but errors. i am trying to use "while" in this. here part of the program.
System.out.println("enter a number to convert");
iInput = Integer.parseInt(keyboard.readLine());

System.out.println("Enter units (M or K)");
cUnit = (char);
double dResult;
String strResult;
while ((cOption == 'Y') || (cOption == 'y')){

if ((cUnit == 'm') || (cUnit == 'M'))
{ //do math to determine Miles.
dResult = KMToMiles(iInput);
strResult = "Miles converted is ";
System.out.println(strResult + dResult);
else if ((cUnit == 'k') || (cUnit == 'K'))
{ //do math to determine Kilometers.
dResult = MilesToKM(iInput);
strResult = "Kilometers converted is ";
System.out.println(strResult + dResult);

please help.

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43935
Welcome to the Ranch.

What sort of errors are you actually getting? How do you get cOption?
And why are you using, rather than the much simpler java.util.Scanner class? Actually requires you to catch (or throw) an IOException; you haven't handled a checked exception, which you might not know about if you are really on the beginner's forum, so you would again b better finding about java.util.Scanner, which definitely has nextInt() and nextDouble() methods, so it probably has a nextChar() method too.
You set it up like this

And you don't seem to have any way of getting 'y' or 'n' into your while loop, so if you ever start it off, you can't stop it.

Any help??

Have you checked out Aspose?
subject: loop problem
It's not a secret anymore!