Link to home
Start Free TrialLog in
Avatar of jkteater
jkteaterFlag for United States of America

asked on

Checking a map for a null value

I have a method today that preforms a check to see if a value exist

//////////////////////////////////////////////////////////////////////////
   //                      prlvalueCheck()                                 //
   //////////////////////////////////////////////////////////////////////////
   public boolean prlValueCheck() {
      boolean prlValue = true;
       
      boolean matrixDest = dscheck;
          
      for (int i = 0; i < rds.size(); i++)  {
          try {
             String value = rds.get(i).rev.getRelatedComponent("IMAN_master_form_rev").getStringProperty("PRL");
             String value1 = rds.get(i).valuePRL;
             
             
             if ((value == null || value.trim().isEmpty()) && matrixDest == true) { 
                prlValue = false;
                break;
             }          
             
             if ((value == null || value.trim().isEmpty()) && value1.trim().isEmpty()) { 
                prlValue = false;
                break;
             }
          }
          catch (TCException e) {
             e.printStackTrace();
         }
      }
      return prlValue;
   }// end prlValueCheck()

Open in new window


What has changed

String value1 = rds.get(i).valuePRL;

Open in new window


Will now be checked from a map.  I understand that if the key is not created there is not a value - so something like

if(pmap.get(rds.get(row).rev) != null), 

Open in new window

could be used in place of String value 1

How should I address the logic for
 
 if ((value == null || value.trim().isEmpty()) && value1.trim().isEmpty()) { 

Open in new window


Something Like

value 1 = pmap.get(rds.get(row).rev)

then
 
 if ((value == null || value.trim().isEmpty()) && value1 == null)) { 

Open in new window

Avatar of jkteater
jkteater
Flag of United States of America image

ASKER

here is the map method

//////////////////////////////////////////////////////////////////////////
   //                             prlMap()                                 //
   //////////////////////////////////////////////////////////////////////////
   public void prlMap(){
      pmap = new HashMap<TCComponentItemRevision,String>();        
      for (int i = 0; i < rds.size(); i++)  { 
           pmap.put(rds.get(i).rev, rds.get(i).valuePRL);
      }         
   }//end prlMap

Open in new window

if( value == null || (value.trim().length() == 0 && value1.trim().length() ==0)){

//do something..

}
I need to change this

String value1 = rds.get(i).valuePRL;

To check and see if there is a value for valuePRL in the map.  So I assume if there is not a key created, then there is not a value.  So I want to change the above to check the map for a key
Something like this - I think

if(pmap.get(rds.get(row).rev) != null) {
prlMapCheck = true;
}'

Then I need to remove value1.trim().isEmpty() from
if ((value == null || value.trim().isEmpty()) && value1.trim().isEmpty()) { 
                prlValue = false;
                break;
             }

Open in new window

and add the value for the map check above.
so what is your question ?

Yes, if there is no key in the map then
map.get(key) will return null for such key

Where do you see the  problem?
how would you go about checking the map and then assigning the value in the if logic
So you want to chek if the map has a value associated with certain key, and if it does not you want to add the key -value pair for such key - this is what you want to do?

Please, explain in words what it is that you want to accomplish
right now

String value1 = rds.get(i).valuePRL;

is getting the value for valuePRL from the ArrayList.  
Then in the if statement it is checking to see if value1 is empty

I am now using a map to store the value of valuePRL with a key of revision number

String value1 = rds.get(i).valuePRL; <- this needs to be replaced with something checking to see if there is a valuePRL value in the map for the specific key

Once I have a value for valuePRL, I need to replace
value1.trim().isEmpty() in the if statement with something that says value1 is false
ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial