简体   繁体   中英

Java While-Loop with Multiple Conditions: Data Validation Issue

I'm creating this Java program which does the following:

/*Prompt user to enter miles and miles per hour.
Display approximate travel time in hours and minutes.
Accept decimal entries.
Prompt user to continue (if user enters “y” or “Y”).
*Must* perform data validation: a. only numbers, b. miles range (> 0 and no more than 3000), c. MPH (> 0 and no more than 100).
Hint: Use integer arithmetic and division and modulus operators to calculate hours and minutes.

Example: miles: 100, MPH 65: 1 hr(s) 32 min(s)*/  

I am able to have the correct end result, but I am having an issue with data validation. An error response should occur when there is an input of a letter, a number less than 0, or a number greater than 3000 when it asks for how many miles (The same should work when asked for MPH, but I was going to edit that when I got the data validation for miles working).

I ended up putting all that validation in one while-loop, and it works fine for the first two conditions (which are if a letter and a number greater than 3000 is entered), but whenever the third condition and the correct input (a number between 0 and 3000) are entered, the program won't immediately accept it, and the input would have to be entered multiple times (see last code block below of terminal output because I couldn't put a picture).

Thank you for any guidance!

import java.util.Scanner;

public class timetravel
{
   public static void main(String[] args)
   {

  double miles;
  double MPH;
  double calculate;
  double hours;
  double minutes;
  char answer; 

  System.out.println("This program displays the approximate travel time in hours and minutes.");
  System.out.println("It accepts decimal entries and MUST perform data validation: a. only numbers, b. miles range (>0 and no more than 3000), c. MPH (> 0 and no more than 100).");
  System.out.println();

  do {
  Scanner sc = new Scanner(System.in);    
  System.out.print("Enter miles: ");
  while((!sc.hasNextDouble()) || sc.nextDouble() > 3000 || sc.nextDouble() < 0){

      System.out.println("Not a valid input.");
      System.out.print("Please enter only numbers greater than 0 and less than 3001: ");

      sc.next();         
    }
  miles = sc.nextDouble();


 System.out.print("Enter MPH: ");
  while(!sc.hasNextDouble()){
        System.out.println("Not a valid input.");
      System.out.print("Please enter only numbers greater than 0 and less than 3001: ");           
      sc.next(); 
    }

  MPH = sc.nextDouble();


  calculate = (miles / MPH);
  hours = Math.floor(calculate);
  minutes = (calculate * 60) % 60; 
  minutes = Math.floor(minutes);    


  System.out.println("Miles: " + miles + ", MPH " + MPH + ": " + hours + " hr(s) " + minutes + " min(s)");
  System.out.println("This is calculate: " + calculate);

  System.out.println("Would you like to continue?: ");
  answer = sc.next().charAt(0);
  answer = Character.toUpperCase(answer);    
  } while(answer == 'Y');    

  }

} 

Terminal Output

Not a valid input.
Please enter only numbers greater than 0 and less than 3001: 3002
Not a valid input.
Please enter only numbers greater than 0 and less than 3001: -1
-1
-1
Not a valid input.
Please enter only numbers greater than 0 and less than 3001: 30
30
30
30
Enter MPH:

I can spot one problem in your while loop. You want to check if the user input is in range, but you read input 2 times with sc.nextDouble() . If sc.nextDouble() > 3000 is true then you get a next value with another call of sc.nextDouble() . That should explain the behavior of your program.

You should check if there is nextDouble with sc.hasNextDouble() then save it to a variable, like double input = sc.nextDouble() and then check for range with input if (input > 0 && input <= 3000) break; so you just break out of loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM