Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regarding variable scope within conditionals

Status
Not open for further replies.

johndoe3344

Programmer
Jul 5, 2007
13
US
I'm trying to make a program which has a menu doing two things:

(1) Read data from an input file
(2) Write certain pieces of the most recently read data to an output file
(3) Exit

The user needs to be able to keep repeating choice 1 for as long as he/she wants, and then use choice 2 to write information to a text file. The user will keep coming back to the menu until he/she selects (3) Exit.

So I end up using code like String a = inFile.next; in option 1. And then in option 2, I try to do something like outFile.println(a); and it doesn't work because the variable's scope ends at where the curly brace ends for the conditional.

So is there a way that I can accomplish what I need to do?
 
Declare the variable outside the curly brackets.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top