I created an if-then condition which controls the flow of my program. In this case, if the user passes a certain date, then the program takes one flow. If the user passes another date, then the program takes a separate flow. I am using JXLS and I have created a Hashmap which takes advantage of whichever dataset is passed to it.
One small problem though. The variable "dataset" which is what I am using in my XLS spreadsheet, will either come from either the if(true) part of my if-statement, or the if(false) part of my if-statement.
But, as you can see in the colorized window, the dataset variable found in the if statement is grayed out and only the one outside of it is recognized by my Hashmap. How do I get around this? (please see attached imgur image)
Mark Richardson wrote:
Can you please elaborate on this? I'm a very junior developer so I'm not entirely sure what you mean... even though I have the basic idea that scope refers to whatever is within the curly braces.
Well, simply, local variables declared in a curly brace can only be used in that curly brace (after the declaration). So, if you want to have variables that is used outside of a specific curly brace, one option is to move the declaration to a curly brace (outer) that cover all the code that needs it.