Re: variable scope question
peterbooth <petermbooth326@gmail.com> wrote in
news:b459b0ec-8a4d-465f-8828-14d7f5691242@z19g2000vbz.googlegroups.com:
One of my students is having trouble with a Java program and I was
hoping you could answer a quick question. We are currently working
with try/catch blocks.
The student wrote a program (code is below) where she initialized and
used a few variables in the 'try' block and then used them again in
one of her 'catch' blocks.
When she compiled her program she received a message that the
variables "fout" and "inFileName" could not be found. I did a little
You failed to mention outFleName, which has the same problem.
research (because this is all new to me also) and I found an internet
reference that says that variables initialized within 'try' blocks
cannot be seen within 'catch' blocks. So I moved the initialization
of these two variables outside the try block. Now the compiler
You didn't move the initialization. You moved the definition.
(BlueJ) is saying that the two variables "might not have been
initialized".
I'm not sure how to proceed. Can anyone offer any guidance?
Peter Booth
-------------------------------------------------------------------------
---------------------------------- Her code (with initializations moved
outside of the try block):
import java.util.Scanner;
import java.io.*;
public class MonthAbbreviations1
{
public static void main(String[]args)
{
PrintWriter fout;
String inFileName;
String outFileName;
Change these three lines (above) to:
PrintWriter fout = null;
String inFileName = null; // or String inFileName = "" ;
String outFileName = null; // or String outFileName = "" ;
and the compiler complaint will go away. This provides both definition and
initialization. This won't help, however, if you use one of them in a
catch block and it hasn't been set up properly first.
try{
int start;
int stop;
int monthNumber;
final String MONTH_TABLE = "JanFebMarAprMayJun" +
"JulAugSepOctNovDec";
String monthAbbrev;
System.out.println("Enter input file name: ");
Scanner kbd = new Scanner(System.in);
inFileName = kbd.nextLine();
File inFile = new File(inFileName);
Scanner fin = new Scanner(inFile);
System.out.println("Enter output file name: ");
outFileName = kbd.nextLine();
File outFile = new File(outFileName);
fout = new PrintWriter(outFile);
while (fin.hasNextDouble())
{
monthNumber = fin.nextInt();
start = (monthNumber - 1)*3;
stop = start + 3;
monthAbbrev = MONTH_TABLE.substring(start,stop);
fout.println("Month #" + monthNumber + " \nbegins with
'" + monthAbbrev +"'.");
}
fin.close();
fout.close();
System.out.print("Done. See " + outFileName);
}
catch (IllegalArgumentException iae)
{
fout.println("***");
System.err.println( iae.getMessage() );
}
catch (FileNotFoundException fnfe)
{
System.err.println("Failed to open file " + inFileName + "
or " + outFileName);
fnfe.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
I don't know why you are catching IllegalArgumentException and Exception.
This is unnecessary and just making the code harder to read.
Good Luck!