• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

loop problem

 
John Saito
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50180
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic