3

This is my current code. What I would like to do is limit the users input to only int. If a double is entered, I'd like a line printed "Please enter a whole number." I've tried to play around with scanner.hasNextint but haven't had much success. Is there a way to ignore double as an input entirely and have it round?

Thanks in advance!

public class BMI extends DecimalFormat{

    public static void main(String[] args) {
        int weight;
        int height;
        double bodyMassIndex;

        DecimalFormat dfWithTwoDecimalPlaces;
        Scanner scanner;
        scanner = new Scanner(System.in);
        System.out.print("What is your weight in kilograms (kg): ");
        weight = scanner.nextInt();
        System.out.print("What is your height in centimeters(cm): " );
        height = scanner.nextInt();
        bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );


        dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
        System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));

    }

}

2 Answers2

2

Create a user defined method getNextInt() as bellow:

/**get next integer*/
public static int getNextInt(Scanner scanner) {
    while (!scanner.hasNextInt()) {
        scanner.next();
    }
    return  scanner.nextInt();
}    

public static void main(String[] args) {
    int weight;
    int height;
    double bodyMassIndex;

    DecimalFormat dfWithTwoDecimalPlaces;
    Scanner scanner;
    scanner = new Scanner(System.in);
    System.out.print("What is your weight in kilograms (kg): ");
    weight = getNextInt(scanner);//call user-defined method
    System.out.print("What is your height in centimeters(cm): " );
    height = getNextInt(scanner);//call user-defined method
    bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );


    dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
    System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));

}

You can add a message inside while loop like this:

while (!scanner.hasNextInt()) {
    scanner.next();
    System.out.println("Please enter a whole number");//message
}
mmuzahid
  • 2,252
  • 23
  • 42
  • This didn't work for me but I didn't have time to fully try and work out the errors that were returned. The assignment that I have didn't call for this, however I had to turn it in, so I turned it in as is and will continue to work on this in my freetime. I appreciate everyones response! Thank you! Ill reply if I get stuck and ill post the errors that I have. – Jack Pavlov Jan 19 '16 at 04:22
0

1.use try {} catch{} block (reference java exception handling)

public class MySource extends DecimalFormat{
public static void main(String[] args) {
     int weight;
        int height;
        double bodyMassIndex;

        DecimalFormat dfWithTwoDecimalPlaces;
        Scanner scanner;
        scanner = new Scanner(System.in);

        while(true) {
            try {
                System.out.print("What is your weight in kilograms (kg): ");
                weight = scanner.nextInt();
                System.out.print("What is your height in centimeters(cm): " );
                height = scanner.nextInt();
                break;
            }catch(InputMismatchException e) {
                System.out.println("Please enter a whole number.");
                scanner.nextLine();
            }
        }


        bodyMassIndex = (weight / Math.pow(height/100.0, 2.0) );


        dfWithTwoDecimalPlaces = new DecimalFormat ("0.00");
        System.out.print("Your Body Mass Index is: " + dfWithTwoDecimalPlaces.format(bodyMassIndex));
}

}

sh. park
  • 1
  • 1