• Post Reply Bookmark Topic Watch Topic
  • New Topic

while loop, if and return statement making me go round and round in circles  RSS feed

 
vin gun
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

My issue is related to simple java while loop, if statement and return statement. Please let me know if you have any questions
Please look at the comments for a description of the issue. I have pasted more code than necessary just to make sure there are no gaps.

Thanks all for you help,

// this method has table that has rows in it. The program checks in the while loop if there are any more rows ( for implementing validation and logic)
public String addRequest()
{
String validationResult;
Iterator<MappingAttribute> entries = mapListHolder.iterator();


String source = getS1cSource().getValue().toString();

SaveMappingRequest mappingRequest = new SaveMappingRequest(getSessionCatalog(), true, source);


while (entries.hasNext())
{
MappingAttribute row = entries.next();



else if (ItemRowModel.GHI_FIELD_VALUE.equals(source)) //ignor this
{
validationResult = validateGHISource(row);
if (validationResult.equalsIgnoreCase("fail"))
return NR_THIS_PAGE;
}


else if (ItemRowModel.DEF_FIELD_VALUE.equals(source)) //ignor this
{
validationResult = validateDEFSource(row);
if (validationResult.equalsIgnoreCase("fail"))
return NR_THIS_PAGE;
}




else if (ItemRowModel.ABC_FIELD_VALUE.equals(source) ) //I am testing this method.
{
validationResult = validateABCSource(row);
if (validationResult.equalsIgnoreCase("fail"))
return NR_THIS_PAGE;
}




boolean result = mappingRequest.addRequest(row);


if (!result)
{
JsfUtils.addErrorMessage("", FacesMessage.SEVERITY_ERROR, "One or more value(s) missing", "One or more value(s) missing");
return NR_THIS_PAGE;
}
}

// may ignore rest of the method


if (getSessionCatalog().getCurrentRequest().getRequestItemList().size() == 0)
{
JsfUtils.addErrorMessage("", FacesMessage.SEVERITY_ERROR, "One or more value(s) missing", "One or more value(s) missing");
return NR_THIS_PAGE;
}


mappingRequest.saveRequest();
initView();


JsfUtils.addErrorMessage(null, FacesMessage.SEVERITY_INFO, MsgUtils.getRequestSuccessSubmit(), null);
return NR_DASHBOARD;
}




public String validateABCSource(MappingAttribute aRow)
{


MappingAttribute row = aRow;
String result = "pass";
ArrayList al = new ArrayList();
al.add("123");
al.add("456");
al.add("789");


String acc = row.getSourceAccountNum();
String companyCode = row.getSourceCompanyCode();
String costCenter = row.getSourceCostCode();
String subAccount = row.getSourceSubAccount();




if ( (acc != null) && (!acc.equals("")) )
{
if( (acc.length() <4) || (acc.length() >5) || (!isAlphaNumeric(acc)) )
{
JsfUtils.addErrorMessage("", FacesMessage.SEVERITY_ERROR, "Account Number For ABC Should Be Minimum 4 and Maximum 5 Characters In Length!", "Account Number For ABC Should Be Minimum 4 and Maximum 5 Characters In Length!");
return result = "fail";
}
}


if ( (companyCode != null) && (!companyCode.equals("")) )
{
if ( !(al.contains(companyCode)) )
{
JsfUtils.addErrorMessage("", FacesMessage.SEVERITY_ERROR, "Valid Company Codes for ABC are 123, 456, 789. Please revise!", "Valid Company Codes for ABC are 123, 456, 789. Please revise!");
return result = "fail";
}


}



if ( (costCenter != null) && (!costCenter.equals("")) )
{
if ( (costCenter.length() != 3) ||(!isNum(costCenter)) )
{
JsfUtils.addErrorMessage("", FacesMessage.SEVERITY_ERROR, "Cost Center for ABC should be 3 digits in length!", "Cost Center for ABC should be 3 digits in length!");
return result = "fail";
}
}


/**ISSUE: I am testing by filling out just one row in the table.*
If I remove the block below, all is fine.


If I keep it: it makes the program throw "please enter a cost center" error when everything is provided in the fields.
What is happening: I saw in debugger that the program executes perfectly for the first row but when it reaches the while statement it thinks that there is one more row and gets inside the loop. All the values are null now since there is actually nothing in the second row. Program reaches the block below and gets inside it and throws the error. */
/* *if ( (costCenter == null) || (costCenter.equals("")) )
{


JsfUtils.addErrorMessage("", FacesMessage.SEVERITY_ERROR, "Please enter a Cost Center!", "Please enter a Cost Center!");
return result = "fail";


}*/

return result;

}

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!