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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With