Search code examples
javaarraysif-statementarraylistnested-for-loop

How to Compare 100 String records which should have either 3 values i.e. true,False or null


I am trying to compare the list of 100 records should have either "true" or false or null .

I have taken all the value of the list in Array which have 100 records with value for e.g [0]-true ,[1]-true,[2]-false, ... and so on.

Now i want to create a method to compare the list of values should atleast have any one of the three values i.e. either true,false or null. If this condition is satisfied then the method should return true.

and if all the list of values are false then the it should return false.

But the method i have created always check the first index value .

Here the list is having string values in it .Could someone please help me to get this assignment done . Thank you

public boolean isAllRecordsAreValid (String []list) {
    boolean ret= false;   
    for (int i=0;i<list.length; i++){
        if ( list[i].equals("true")&& list[i].equals(null)&&list.equals("false")){
            return false ;
        }
    return true;
    }
    return ret;
}

Solution

  • As far as I understand, you have two cases to cover three scenarios. So it is not clear on your question what value must be returned if your array contains different values than "at least one true, false and null" or "all false". So what you can do is to raise an exception for the third scenario (e.g. the array contains only true values or only false values, etc)

    Also it is not clear if you refer to the String "null" or the null value. Without this information it's hard to understand how to better help but I think you can use this code to better guide yourself:

    public class A {
    
        public boolean isAllRecordsAreValid (String []list) {
    
            boolean hasNull = false;   
            boolean hasTrue = false;   
            boolean hasFalse = false;   
            boolean allAreFalse = true;
    
            for (int i=0;i<list.length; i++) {
    
                if( list[i] == null || list[i].equals("null") ){
                    hasNull = true;
                    allAreFalse = false;
                } else if( list[i].equals( "true" ) ){
                    hasTrue = true;
                    allAreFalse = false;
                } else if( list[i].equals( "false" ) ){
                    hasFalse= true;
                }
    
                if( hasNull && hasTrue && hasFalse ) {
                    return true;
                }
    
            }
            
            if( allAreFalse ){
                return false;
            }else{
                throw new UnsupportedOperationException("The array is not supported");
            }
    
        }
    
        public static void main(String[] args) {
            String[] test1 = {"true", "false", null};
            String[] test2 = {"true", "false", "null"};
            String[] test3 = {"false", "false", "false"};
            String[] test4 = {"true", "true", "true"};
    
            A a = new A();
    
            System.out.println( "test1: "+ a.isAllRecordsAreValid( test1 ) );
            System.out.println( "test2: "+ a.isAllRecordsAreValid( test2 ) );
            System.out.println( "test3: "+ a.isAllRecordsAreValid( test3 ) );
            System.out.println( "test4: "+ a.isAllRecordsAreValid( test4 ) );
        }
    }