Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @author Wesley Liu
- */
- import java.util.Scanner;
- public class BMICalculatorApp {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- boolean hello = false;
- while(!hello){
- System.out.println("Please type your name. ");
- String name = input.next();
- System.out.println("Please type your weight.");
- double weight = 0.0;
- boolean isValidDouble = false;//indicates if our input entry is a double
- boolean isValidRange = false; //indicates if our input entry is in the right range
- while(!(isValidDouble==true && isValidRange == true))
- {
- try {
- //statements may throw an exception should be in try block
- //take input entry from user
- weight = input.nextDouble();//may throw an exception
- //System.out.println("after nextint()");//will be skipped if an exception is thrown
- isValidDouble = true;
- } catch (Exception e) {
- input.next();//discard the incorrecly entered value from the pipe
- //statements to handle exceptions
- System.out.println(e);
- System.out.println("Please type a valid weight, try again.");
- }
- //if it is a double
- if (isValidDouble) {
- //start to determine if it is in the right range [30, 300]
- if (weight < 30 || weight > 300) {
- System.out.println("Invalid, please type a weight between 30 and 300.");
- }
- else
- {
- isValidRange = true;//we have the valid range entry
- }
- }
- }
- System.out.println("Please type your height.");
- double height = 0.0;
- isValidDouble = false;
- isValidRange = false;
- while(!(isValidDouble==true && isValidRange == true))
- {
- try {
- //statements may throw an exception should be in try block
- //take input entry from user
- height = input.nextDouble();//may throw an exception
- //System.out.println("after nextint()");//will be skipped if an exception is thrown
- isValidDouble = true;
- } catch (Exception a) {
- input.next();//discard the incorrecly entered value from the pipe
- //statements to handle exceptions
- System.out.println(a);
- System.out.println("Please type a valid height, try again.");
- }
- //if it is a double
- if (isValidDouble) {
- //start to determine if it is in the right range [30, 300]
- if (height < 30 || height > 100) {
- System.out.println("Invalid, please type a height between 30 and 100.");
- }
- else
- {
- isValidRange = true;//we have the valid range entry
- }
- }
- }
- BMICalculator calculator = new BMICalculator(name, weight, height);
- String category = calculator.calculateBMIandPrintCategory(calculator.getName(), calculator.getWeight(), calculator.getHeight());
- System.out.println(category);
- System.out.println("Continue? y/n");
- String choice = input.next();
- if(choice.toUpperCase().equals("N"))
- {
- hello = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement