I tried to declare s.next() after the try-catch block but it wouldn’t work! The s would only have the drop down if it is inside the try block.
I don’t want to lump Parsing inputs, take appropriate actions all into the try block, because they wouldn’t throw the FNFE and IOE. What can I do here?
public static void main(String[] args)
{
// TODO Auto-generated method stub
//Open file; file name specified in args (command line)
try{
FileReader freader = new FileReader(args[0]);
Scanner s = new Scanner(freader);
}catch(FileNotFoundException e){
System.err.println("Error: File not found. Exiting program...");
e.printStackTrace();
System.exit(-1);
}catch(IOException e){
System.err.println ("Error: IO exception. Exiting...");
e.printStackTrace();
System.exit(-1);
}
// if i try to declare s.next() here it would not work
I think you mean you want to use s.next() and that it won’t work.
to do that, declare s as a variable outside the try/catch block, set it to null there. Then assign it where you assign it now, but without the declaration. If my assumption is correct, your problem is that s is no longer an active variable outside the try/catch, because it is declared within that block.