Hi Everyone

I have been hard at work on this Java program involving bicycle gears, one of the problems is the while loop, should a user enter a negative number in the inputs then i want the user to re enter it until a positive number comes up however the program is also recognising positves as negatives, i also want the program to print the previous ratio entred but cant think of a formula that would display it.

Sorry if i may have confused you all but it should all make clear in my code, im currenty a beginner programmer and would like to get past these problems, if you have encounted any problems or better alternatives please let me know :-)

Thanks anyway

public class ProcessBicycleGears {


public static void main(String[] argv) {

// put your local declarations here
		
double radius;
int frontteeth;
int rearteeth;
double eradius;
double ratio;
double pradius;
     
    
		// Prompt and read the inches
		
		System.out.println("Please enter the inches");
		radius = UserInput.readDouble();
		
		// Prompt and read the front teeth
		
		System.out.println("Type the front teeth");
		frontteeth = UserInput.readInt();
		
		  while (frontteeth <=0);
      {
          System.out.println("Error, negative input");
          
          System.out.println("Type the front teeth");
		  frontteeth = UserInput.readInt();
          
          // Prompt and read the rear teeth
		
		System.out.println("Type the rear teeth");
		rearteeth = UserInput.readInt();
		
		  while (rearteeth <=0);
      {
          System.out.println("Error, negative input");
          
          System.out.println("Type the rear teeth");
		  rearteeth = UserInput.readInt();
          
          // Compute and print
		
		eradius = (radius*frontteeth)/rearteeth;
		
		System.out.println("The effective radius for radius" +radius+ "and sprockets" +frontteeth+ "and" +rearteeth+ "is" +eradius);
      
		System.out.println("Ratio to previous" +ratio);
		

	} // end of main

} // end class
}
}
while (frontteeth <=0);
...
	  while (rearteeth <=0);

The semicolon at the end of this line is an instruction. It means "do nothing". So if frontteeth or rearteeth is le 0, it'll hang at one or the other of these points.

For input validation, I think a do...while loop is the most readable. do...while always executes the first time, which is the correct behavior for simple input checking:

do {
prompt;
get input;
} while (input is not valid);

For added style points, you can tell the user what they did wrong:

do {
prompt;
get input;
if (input is not valid)
 {
   complain;
 }
} while (input is not valid);

To retain the previous value, you just need to assign the ratio to a variable (such as the one, ratio that you're using now), and run the whole thing in a loop. If you want to retain the previous value across sessions, that's a little more advanced - not terribly so, but a little.

This question has already been answered. Start a new discussion instead.