• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to refer to a variable in an if statement outside of the if statement?

 
Ranch Hand
Posts: 185
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)




http://imgur.com/wsTADfZ   -  You can view the IDE screen here as I am seeing it  Notice the second grayed-out "dataset."
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to move dataset to the outer scope.
 
Mark Richardson
Ranch Hand
Posts: 185
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:You'll need to move dataset to the outer scope.



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.
 
author
Posts: 23834
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!