Monday 15 February 2010

java - pulling a variable from a bracket section -



java - pulling a variable from a bracket section -

i have got while statement contains many different if statements add together value array. value in array has been pulled file.

int variable 1 int variable 2 while(scanner.next()) { if variable 1 { } if variable 2 { } }

the values add together each line , display total line only. want total values each line add together give overall total.

the problem when seek utilize variable 1 or 2 after closing while bracket, error. presume because not in same block while statement? how solve problem?

this similar need:

int variable 1 int variable 2 while(scanner.next()) { if variable { } if variable 2 { } } int overall total = variable 1 + variable 2; system.out.println(overalltotal);

the brackets in java, defines block. each block has own scope , inherits scope of parent block.

therefore, when define new variable within block, alive (or accessed) on block's scope (and kid scopes), , not external scopes.

please, take @ article, specially @ local variables section.

example

this won't work:

if(something) { //start of if scope //we create somevar on if scope int somevar = 0; } //end of if scope system.out.println(somevar); //you can't access somevar! out of scope

this will work:

//we create somevar on method's scope int somevar = 0 if(something) { //start of if scope //this "if scope" kid scope, inherits parent's scope //can access parent scope somevar = 2; } //end of if scope system.out.println(somevar); //can access somevar! wasn't defined on kid scope

java

No comments:

Post a Comment