This question is a bit confusing so I will try to explain through code and example.
How can I decrement a counter if it meets a condition? Sounds simple right? But what if that method continuously gets evoked from within a for loop so that values passed to it, get reset?
Eample: I have a document with several pages, however some of those pages are blank and I don't want to count them.
For a 5 page document with the first page being blank. I want to accomplish the following behavior:
page 1: nothing...
page 2: 1 of 4
page 3: 2 of 4
page 4: 3 of 4
page 5: 4 of 4
Currently it's including the blank page as part of the page count.
Here is a code sample:
static main() {
// et cetera
for(Page page : pages) {
int pageNumber = page.getPageNo();
int pageCount = page.getTotalPageCount();
form.getTemplate().writePageNumber(page, pageNumber,pageCount);
}
}
So in the main routine I'm calling a method called writePageNumber()
from within a for loop where I pass in currentPageNumber and totalPage count.
public boolean writePageNumber(Page page, int currentPageNo, int totalPageCount) {
if ( !page.isBlank() ) {
this.write(currentPageNo, totalPageCount);
}
else if ( page.isBlank() ){
currentPageNo--;
totalPageCount--;
}
// et cetera
}
Now here in the writePageNumber() method, I get page context and I process. I check to see if the page is not blank then I write the currentPage of totalPages, if it is blank then I decrement the currentPage No and totalPage count...so as to not count the blank page...is this the right way to do it? Not sure
Since the method is called from within a for loop, the values after I decrement them get reset, how to maintain decremented values?
There probably is a better way to do this, any help would be appreciated.
Edit:
Just for clarity, the problem is that writePageNumber() is called from a for loop so the next time writePageNumber is called, the values are reset, so during the second iteration the else if condition is no longer true and the first condition is true so the wrong page number is written