public class ValidateStringLengthExample
{
public static final int VALID = 0;
public static final int TOO_SHORT = 1;
public static final int TOO_LONG = 2;
public int validateStringLength(String inputString)
{
int min = 10; // minimum valid String length
int max = 15; // maximum valid String length
if(inputString.length() < min)
{
return(TOO_SHORT);
}
if(inputString.length() > max)
{
return(TOO_LONG);
}
return(VALID);
}
public void testExamples()
{
int ret = validateStringLength("some input text here");
if(ret == TOO_SHORT)
{
System.err.println("The input length is too short");
}
else
{
// continue with program flow
System.err.println("The input length is not too short");
}
if(ret != VALID)
{
// Perhaps check for more specific error type here.
System.err.println("The input length is not valid");
}
}
public static void main(String args[])
{
ValidateStringLengthExample ex = new ValidateStringLengthExample();
ex.testExamples();
}
}
The input length is not too short The input length is not valid
--- Output Ends ---
- javac ValidateStringLengthExample.java
- java ValidateStringLengthExample