user3164187 user3164187 - 5 months ago 7
Java Question

Avoid multiple if else

I have the following scenario where,

A

String arr[]
as input which can have Integer values,null or empty as a string.
(e.g)
{"230",null,"6",""}


Now,

1. If a null or empty string present in first two entries of arr , it is an invalid scenario print nothing.

2. If a null or empty is present in last two entries of arr, and first two entries are not null and not empty print first two entries.

(e.g) If arr[] is {"2","77",null,"5"} output will be 2,77.

3. If no entry in arr[] is neither null nor empty check if all entries are numeric. If not print nothing.

Now my code goes like this,

simple validation()
{
onlyFirstTwoNotNullOrNotEmpty = false;

if(//Check if first two params are null)
{
//"print nothing" and return
}
else if(check if third **or** fourth param is null)
{
onlyFirstTwoNotNullOrNotEmpty = true; // setting a boolean
}

//Proceed to check empty string
if(//Check if first two params are empty)
{
"print nothing" and return
}
else if(!onlyFirstTwoNotNullOrNotEmpty) //checking if null
{
if(check if third **or** fourth param is empty)
{
onlyFirstTwoNotNullOrNotEmpty = true; // setting a boolean
}
}

//if all values are not null and not empty
//onlyFirstTwoNotNullOrNotEmpty will be still false

String result;

if(onlyFirstTwoNotNullOrNotEmpty )
{
result = checkIfNumeric(0,1); // only first two entries
}
else
{
result = checkIfNumeric(0,arr[arr.length-1]); // for all entries
}

if(!result.equals("success"))
{
return and print nothing
}

if(onlyFirstTwoNotNullOrNotEmpty)
{
print only first two and return;
}
print all entries and return;
}


How can I better optimise this code with less if and else loops? Are there any possibilities via lambda expressions?

I don't want to use a third party API for null or Empty check of a string.

Answer

You code is way more complicated than it needs to be:

You can do it without else clauses, if the code is in its own method, like your code appears to be.

Here it is, with comments:

private static void simpleValidation(String[] arr) {
    // Undefined Rule: If bad input, print nothing
    if (arr.length != 4)
        return;

    // 1: If a null or empty string present in first two entries of arr, it is a invalid scenario print nothing
    if (isNullOrEmpty(arr[0]) || isNullOrEmpty(arr[1]))
        return;

    // 2: If a null or empty is present in last two entries of arr, and first two entries are not null and not empty print first two entries.
    if (isNullOrEmpty(arr[2]) || isNullOrEmpty(arr[3])) {
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        return;
    }

    // 3: If no entry in arr[] is neither null nor empty check if all entries are numeric. If not print nothing.
    if (! isNumeric(arr[0]) || ! isNumeric(arr[1]) || ! isNumeric(arr[2]) || ! isNumeric(arr[3]))
        return;

    // Undefined Rule: Print all values
    System.out.println(arr[0]);
    System.out.println(arr[1]);
    System.out.println(arr[2]);
    System.out.println(arr[3]);
}
private static boolean isNullOrEmpty(String value) {
    return (value == null || value.isEmpty());
}
private static boolean isNumeric(String value) {
    return value.matches("\\d+");
}

If you want it with else clauses and no return statements, e.g. because there is more code after, it's this simple (using same two helper methods):

private static void simpleValidation(String[] arr) {
    if (arr.length == 4 && ! isNullOrEmpty(arr[0]) && ! isNullOrEmpty(arr[1]))
        if (isNullOrEmpty(arr[2]) || isNullOrEmpty(arr[3]))
            System.out.printf("%s%n%s%n", arr[0], arr[1]);
        else if (isNumeric(arr[0]) && isNumeric(arr[1]) && isNumeric(arr[2]) && isNumeric(arr[3]))
            System.out.printf("%s%n%s%n%s%n%s%n", arr[0], arr[1], arr[2], arr[3]);
}
Comments