HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaModerate

Shorter way of prompting for a height of x feet y inches?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
heightpromptingwayfeetforshorterinches

Problem

I'm currently working on a BMI calculator and just wondered if there was a shorter or more condensed version of doing the if validating statement.

I have currently done it this way for both Height and Weight, but I find it to be a mess and it seems to cluttered. There must be a way of shortening this down or condensing it.

boolean Hresult = false;
int resulth = 0;
while(Hresult == false) {

    System.out.print("\nPlease enter your feet: ");
    int heightft = scanner.nextInt();

    if (heightft >=2 && heightft =0 && heightin <=11 ){
        System.out.println("You entered: " + heightin);
        Hresult = true;
    }
    else {
        System.out.println("Please try again");  
        Hresult = false;
    } 
    int resultin = (heightft * 12);
    resulth = (resultin + heightin);
}
return resulth;

Solution

The short answer is: Yes, there are ways to improve it!

Coding conventions

First of all, you are not following some Java coding conventions of indentation and where to put }-characters and similar.

Error messages

"Please try again" is not a message that really describes what went wrong. If I would see that message my first reaction would be to try to enter the exact same value again, which would just give me the same error message.

What your code does

Your code is essentially divided into two parts. The first part is:

System.out.print("\nPlease enter your feet: ");
    int heightft = scanner.nextInt();

    if (heightft >= 2 && heightft <= 7) {
        System.out.println("You entered: " + heightft);
        Hresult = true;
    }
    else {
        System.out.println("Please try again");  
        Hresult = false;
    }


Second part:

System.out.print("Please enter your inches: ");
    int heightin = scanner.nextInt();

    if (heightin >= 0 && heightin <= 11) {
        System.out.println("You entered: " + heightin);
        Hresult = true;
    }
    else {
        System.out.println("Please try again");  
        Hresult = false;
    }


Hresult in the first part of the code essentially has no effect because it will be overwritten by Hresult in the second part.

Now let's see what the common areas of this code are:

  • Show a message



  • Read an int from the Scanner



  • Check the range of the entered value



  • Show what you entered



  • Return whether or not the result was successful.



This could become it's own method.

public static int inputWithExpectedRange(String message, int min, int max) {
    do {
        System.out.print(message);
        int value = scanner.nextInt();

        if (value >= min && value  max);
    return value;
}


Now what you can do is to call this method twice from your previously existing method. It is also likely that you will find use for that method when the user should enter his/her weight.

System.out.println(); // this will print a line break *before* entering the input-method.
int feet = inputWithExpectedRange("Please enter your feet: ", 2, 7);
int inches = inputWithExpectedRange("Please enter your inches: ", 0, 11));
return feet * 12 + inches;


Violà.

An entirely different approach

As mentioned in the comments to your question, you can also use Regular Expressions to verify input and parse it. Here is a brief example of how to use Regular Expressions to input this. I will let you work on the details for it (such as making sure feet and inches are in the proper ranges). And you probably also want it inside some kind of loop (either while or do-while) to make sure that the user enters acceptable values.

String input = scanner.nextLine();
    String regex = "(\\d+)'(\\d+)\"";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    if (!matcher.find()) {
        System.out.println("Input was not in normal feet and inch format");
    }
    int feet = Integer.parseInt(matcher.group(1));
    int inches = Integer.parseInt(matcher.group(2));


If you enter 5'11" the variable feet will be 5 and inches will be 11.

Code Snippets

System.out.print("\nPlease enter your feet: ");
    int heightft = scanner.nextInt();

    if (heightft >= 2 && heightft <= 7) {
        System.out.println("You entered: " + heightft);
        Hresult = true;
    }
    else {
        System.out.println("Please try again");  
        Hresult = false;
    }
System.out.print("Please enter your inches: ");
    int heightin = scanner.nextInt();

    if (heightin >= 0 && heightin <= 11) {
        System.out.println("You entered: " + heightin);
        Hresult = true;
    }
    else {
        System.out.println("Please try again");  
        Hresult = false;
    }
public static int inputWithExpectedRange(String message, int min, int max) {
    do {
        System.out.print(message);
        int value = scanner.nextInt();

        if (value >= min && value <= max) {
            System.out.println("You entered: " + value);
        }
        else {
            System.out.println("Expected a value between " + min + " and " + max + ". Please try again");  
        } 
    }
    while (value < min || value > max);
    return value;
}
System.out.println(); // this will print a line break *before* entering the input-method.
int feet = inputWithExpectedRange("Please enter your feet: ", 2, 7);
int inches = inputWithExpectedRange("Please enter your inches: ", 0, 11));
return feet * 12 + inches;
String input = scanner.nextLine();
    String regex = "(\\d+)'(\\d+)\"";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);
    if (!matcher.find()) {
        System.out.println("Input was not in normal feet and inch format");
    }
    int feet = Integer.parseInt(matcher.group(1));
    int inches = Integer.parseInt(matcher.group(2));

Context

StackExchange Code Review Q#35235, answer score: 17

Revisions (0)

No revisions yet.