How to check if a string is in a specific range. (Java)

I need to check if a given string is between a range of rows.

I have two lines in the file (stringA and stringB), my program will allow me to enter a name and test if it matches the range of stringA and stringB.

For example, if the file had Adam and William, and I tested John, he would return the truth, unlike Zayn, who goes outside and returns the lie.

I hope this makes sense, and thanks for any help.

Ryan.

+5
source share
1 answer

You can use the String class method compareToto achieve this functionality as follows:

public boolean inRange(String lowerBound, String upperBound, String input) {
    // (First, be sure to check for null values)
    return input.compareToIgnoreCase(lowerBound) >= 0 && input.compareToIgnoreCase(upperBound) <= 0
}
+10
source

All Articles