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)); } }