I create a variable called "file_name" in both legs of a try-catch block -- so, it should be available whether an error is thrown or not.
But alas, when I try to use the "file_name" variable in my next try-catch block, I get "cannot find symbol".
package timelogger;
import java.io.IOException;
public class TimeLogger {
public static void main(String[] args) throws IOException {
try {
String file_name = args[0];
}
catch (IndexOutOfBoundsException e){
String file_name = "KL_Entries.txt";
}
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for ( i=1; i < aryLines.length - 2; i++ ) { //-2 because last two lines not entries
//System.out.println( aryLines[ i ] ) ;
}
System.out.println(aryLines[1].charAt(24));
System.out.println(aryLines[1].charAt(48));
}
catch (IOException e){
System.out.println(e.getMessage());
}
}
}
I tried doing "public String file_name = ..." instead, but that gave error "illegal start of expression" or something.
How do I make this code compile? I feel like I'm missing something silly.
EDIT: Found this, indicating that variables are local to try-catch blocks. So, fixed the problem by declaring the variable outside the try-catch and then giving it values in the try-catch block.