Advertisement
Guest User

Untitled

a guest
May 25th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /**
  2. * @author Wesley Liu
  3. */
  4. import java.util.Scanner;
  5. public class BMICalculatorApp {
  6. /**
  7. * @param args the command line arguments
  8. */
  9. public static void main(String[] args) {
  10. Scanner input = new Scanner(System.in);
  11. boolean hello = false;
  12. while(!hello){
  13. System.out.println("Please type your name. ");
  14. String name = input.next();
  15. System.out.println("Please type your weight.");
  16. double weight = 0.0;
  17. boolean isValidDouble = false;//indicates if our input entry is a double
  18. boolean isValidRange = false; //indicates if our input entry is in the right range
  19. while(!(isValidDouble==true && isValidRange == true))
  20. {
  21. try {
  22. //statements may throw an exception should be in try block
  23. //take input entry from user
  24. weight = input.nextDouble();//may throw an exception
  25. //System.out.println("after nextint()");//will be skipped if an exception is thrown
  26. isValidDouble = true;
  27. } catch (Exception e) {
  28. input.next();//discard the incorrecly entered value from the pipe
  29. //statements to handle exceptions
  30. System.out.println(e);
  31. System.out.println("Please type a valid weight, try again.");
  32. }
  33. //if it is a double
  34. if (isValidDouble) {
  35. //start to determine if it is in the right range [30, 300]
  36. if (weight < 30 || weight > 300) {
  37. System.out.println("Invalid, please type a weight between 30 and 300.");
  38. }
  39. else
  40. {
  41. isValidRange = true;//we have the valid range entry
  42. }
  43. }
  44. }
  45. System.out.println("Please type your height.");
  46. double height = 0.0;
  47. isValidDouble = false;
  48. isValidRange = false;
  49. while(!(isValidDouble==true && isValidRange == true))
  50. {
  51. try {
  52. //statements may throw an exception should be in try block
  53. //take input entry from user
  54. height = input.nextDouble();//may throw an exception
  55. //System.out.println("after nextint()");//will be skipped if an exception is thrown
  56. isValidDouble = true;
  57. } catch (Exception a) {
  58. input.next();//discard the incorrecly entered value from the pipe
  59. //statements to handle exceptions
  60. System.out.println(a);
  61. System.out.println("Please type a valid height, try again.");
  62. }
  63. //if it is a double
  64. if (isValidDouble) {
  65. //start to determine if it is in the right range [30, 300]
  66. if (height < 30 || height > 100) {
  67. System.out.println("Invalid, please type a height between 30 and 100.");
  68. }
  69. else
  70. {
  71. isValidRange = true;//we have the valid range entry
  72. }
  73. }
  74. }
  75. BMICalculator calculator = new BMICalculator(name, weight, height);
  76. String category = calculator.calculateBMIandPrintCategory(calculator.getName(), calculator.getWeight(), calculator.getHeight());
  77. System.out.println(category);
  78. System.out.println("Continue? y/n");
  79. String choice = input.next();
  80. if(choice.toUpperCase().equals("N"))
  81. {
  82. hello = true;
  83. }
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement