开发者

Help with try/catch and accessing elements within the try

try {
    inFile = new Scanner(file);
}
catch (FileNotFoundException e) {
    System.out.println("FileNotFoundException");
}

I have this code. However, after the try/catch statement I have the following:

whi开发者_Python百科le(inFile.hasNext()) {
}

The compiler is telling me that I have not initialized inFile. Do I need to put all of my code within the try/catch? What am I doing wrong?


Initialize inFile:

Scanner inFile = null;

Edit:

As others have mentioned, you should be careful that you could potentially get a NullPointerException in your while loop. You should consider moving your while loop into the try block as well:

Scanner inFile = null;
...
try {
    inFile = new Scanner(file);
    while(inFile.hasNext()) {
    }    
}
catch (FileNotFoundException e) {
    System.out.println("FileNotFoundException");
}


The compiler is complaining because if new Scanner() throws FileNotFoundException, inFile won't be initialized (BTW very unfortunate variable name). Your loop should be inside try block, which will also increase readability.

try {
    Scanner inFile = new Scanner(file);
    while(inFile.hasNext()) {
        //...
    }
    inFile.close();
}
catch (FileNotFoundException e) {
    System.out.println("FileNotFoundException");
}


If you are getting a compiler error, you probably need to initialize inFile to null.

Note that later in your code you shouldn't assume that inFile is not null, you should always check it:

e.g.

if (inFile != null) {
    while (inFile.hasNext()) {
        ...
    }
}


Yes, you do. If an exception is raised the runtime will print "FileNotFoundException" and will keep running, although inFile will not have been initialized.

You should make the program return when stumbling upon this exception, or else do your operations on infile only when you are sure that it has been initialized correctly.


The try block is not in the same scope as your while loop. Put the while loop inside the try block.


The reason for the warning is that you should set the Scanner to null initially. However you should also move the while loop inside the try block, because if the exception is thrown you don't want that while loop to execute (because inFile will be null).


No your code is fine, however local variables must be intialized so you have to set Scanner inFile = null;

And it is correct what you already have done, because if you move the local variable inside the try/catch statement you will not have access to it because of the scope.

Scanner inFile = null;

try {
    inFile = new Scanner(file);
    //more code
} catch (Exception e) {
    //exception code
}

while (inFile.nextLine()) {
    //loop code
}

If you had an instance variable it would have been automatically set to null, but in this case you have a local variable and then objects need to be initialized before you use it.


Yes, as others have said, initialize inFile to null

However, you will also need to check that inFile actually points a valid file and is not NULL when you get to your loop e.g.

while(inFile!=null && inFile.hasNext()) {
}

Otherwise, you perhaps want to place the whole try-catch in a different loop to make the user select another file? Or just exit the program if the file is invalid? The missing element to the question is how you wish to handle invalid files. Does the program exit or re-prompt the user?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜