Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string lies in a certain range. (Java)

Tags:

java

string

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

I have two strings in a file (stringA and stringB), my program would allow me to input a name and test if it fits in the range of stringA and stringB.

For example if the file had Adam & William, and I tested John it would return true, as opposed to Zane which falls outside and would return false.

I hope this make sense, and thank you for any help.

Ryan.

like image 229
Shaun Avatar asked Aug 28 '12 05:08

Shaun


1 Answers

You can use the String class's compareTo method to 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
}
like image 144
Jon Newmuis Avatar answered Sep 28 '22 08:09

Jon Newmuis