0

The program should do the following:

Write a method called getheartRate that takes no parameters and returns an int (heartRate). This method prompts the user for the patient's heart rate, reads their input from the command line, and returns this value.

Write a method called checkHeartRate that takes an int parameter (the heart rate) and returns a String (result). If the heart rate is above 100, return the value "High". If the heart rate is below 60, return the value "Low". Otherwise return "Normal".

Write a method called printHRResult that takes a String parameter, which is the result from the method checkHeartRate. Print this value to the command line.

Call all three methods from the main method using appropriate parameter passing.

So far I have:

public class UnitSixInClassFall2018 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        UnitSixInClassFall2018 u = new UnitSixInClassFall2018();
        u.getHeartRate();
        System.out.println();
        Scanner scan = new Scanner(System.in);
        u.checkHeartRate(0);
        // END MAIN
    }

    public int getHeartRate(){
        System.out.print("Please enter your heart rate: ");
        Scanner scan = new Scanner(System.in);
        int heartRate = scan.nextInt();
        return 0;
    }

    public void checkHeartRate(int heartRate){
        if (heartRate > 100) {
           String result = "High";
        }
        else if (heartRate < 60) {
           String result = "Low";
        }
        else {
           String result = "Normal";
        }
    }

    public String printHRResults(String result) {  
       System.out.print("Your hear rate is " + result + ".");
       return result; 
    }

}

When this is run, all that is output is "Please enter your heart rate: ". Once I enter an integer, the program ends. What is being done incorrectly?

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92
A_Pacheco
  • 11
  • 2
  • Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – StaticBeagle Nov 01 '18 at 16:59
  • In checkHeartRate don't forget to call printHRResults – Leo Leontev Nov 01 '18 at 17:01
  • `getHeartRate` should not return 0 but `heartRate`. `checkHeartRate` should not declare to return void (nothing) but String, and then `return result;`. And this level of questions is still too beginners I am afraid. – Joop Eggen Nov 01 '18 at 17:07

2 Answers2

0

First of all, you are creating two Scanner objects with the same input type (System.in), which isn't recommended. Instead, just make one Scanner object and use it everywhere in your program. This post has some good info.

An improved version of your code with an improved use of the Scanner object is as follows:

public UnitSixInClassFall2018 {
    private static final Scanner scan = new Scanner(System.in);

    public static void main(String[] args) {
        NewMain u = new NewMain();
        int heartRate = u.getHeartRate();
        System.out.println();
        String result = u.checkHeartRate(heartRate);
        u.printHRResults(result);
        scan.close(); // Close the scanner once you are completely done using it
    }

    public int getHeartRate() {
        System.out.print("Please enter your heart rate: ");
        return scan.nextInt(); // Return the input of the user
    }

    public String checkHeartRate(int heartRate) {
        String result = ""; // Initialize some default value
        if (heartRate > 100) {
           result = "High";
        }
        else if (heartRate < 60) {
           result = "Low";
        }
        else {
           result = "Normal";
        }
        return result; // Return the result calculated from the if-statements
    }

    public void printHRResults(String result) {
       System.out.print("Your heart rate is " + result + ".");
       // Originally this method returned a string but that seems unnecessary
    }
}
0xCursor
  • 2,224
  • 4
  • 13
  • 30
0

You should change this method to return the heart rate like this:

public int getHeartRate(){
    System.out.print("Please enter your heart rate: ");
    Scanner scan = new Scanner(System.in);
    int heartRate = scan.nextInt();
    // Also add this
    scan.close();
    return heartRate;
}

And change this method to return the result:

public String checkHeartRate(int heartRate){
    if (heartRate > 100) {
       return "High";
    }
    else if (heartRate < 60) {
       return "Low";
    }
    else {
       return "Normal";
    }

}

Then in your main method:

// get the heart rate
int heartRate = u.getHeartRate();
// Call the checkHeartRate method
String result = checkHeartRate(heartRate);
// call the printHRResults 
printHRResults(result);

That should solve your problem.

Pritam Banerjee
  • 15,297
  • 10
  • 71
  • 92