This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "loop problem" Watch "loop problem" New topic
Author

loop problem

John Saito
Greenhorn

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) System.in.read();
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.

John
Campbell Ritchie
Sheriff

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

What sort of errors are you actually getting? How do you get cOption?
And why are you using System.in.read(), rather than the much simpler java.util.Scanner class? Actually System.in.read() 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??

CR
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: loop problem
 
Similar Threads
Can you make java program into GUI?
LengthConverter
My program has a bug. Need help
determine least number of keypress
Changing for loop into a while loop