0

Can someone please highlight to me the problem With my main method? I am getting the error exception that scanner is closed once I complete first option and try to enter another?

I think the problem I am having is from the placement of my try catch and finally blocks but not totally sure! Thanks!

/**
 * Scanner used for input within program
 */
public static Scanner scanner = new Scanner(System.in);

/**
 * Main method that provides user with a menu in which each number
 * represents a different task in which they can carry out
 */
public static void main(String[] args) {



int menuOption=0;
do{ 
    try {

        // Declaring var for user Input (defaulted to 0)
        menuOption = showMenu();
        switch (menuOption) {

        case 1:
            add();
            break;
        case 2:
            subtract();
            break;
        case 3:
            generateRandomNumber();
            guessRandomNumber();
            break;
        case 4: // invoke print loop method (use params here to get
                // experience!)
            break;
        case 5: // invoke print sum and average
            break;
        case 6: System.out.println("Quitting Program...");
            break;
        default:
            System.out.println("Sorry, please enter valid Option");

        }// End of switch statement

    } catch (Exception ex) {
        //flush scanner
        scanner.next();
    }

    finally {
        // Finally block ensures scanner is always closed
        scanner.close();
    }

}while(menuOption!=6);

//Exiting message
System.out.println("Thanks for using this Program...");
  • well you're closing the `Scanner` in the finally block so it wont be able to read after the 1st iteration... – Reimeus Nov 30 '13 at 17:07

5 Answers5

0

The problem is that in the first iteration of the loop you are closing the scanner in the finally block. The finally block goes off every time, even if a exception isn't caught.

0

I'm guessing you're using exceptions for program flow (and catch-all at that!)? Don't. Exceptions are very expensive and using them for program flow clutters the code a lot.

You're using scanner.next() in your catch statement: this will ask for new input. But since the finally block is executed right after the catch, you'll immediatly close it.

The finally block will execute regardless of exceptions or not. It is meant to clean up resources, but you're cleaning them too early.

Remove the try-catch (or make it more appropriate) and don't close your scanner in the finally block.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
0

Close the scanner outside of the loop and get rid of the finally

do {
   ...
} while (...)

scanner.close();
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
0

Right now your flow of control looks like this

do {
    try {
        //read from scanner
    }
    finally {
        scanner.close();
    }
} while (menuOption != 6);

but this closes your scanner after first iteration.

You probably should move your do...while() inside try{...} block.

try {
    do{
        //read from scanner
    }
    while (menuOption != 6);
}
finally {
    scanner.close();
}

This way your finally block which ensures closing of scanner will be outside of loop.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • By doing this does that mean that there will be no catch block? –  Nov 30 '13 at 17:20
  • @RNI2013 Why not? If you need one then you can place it after `try` block and before `finally`. But in your catch block you claim to flush scanner and later you invoke `scanner.next()` which most probably shouldn't be placed in `catch` but in `try`. – Pshemo Nov 30 '13 at 17:26
  • So you are saying not to flush the scanner in the catch section of the code, or should scanner be flushed at all? It may be relevant to know that I have try catch in other methods of this program that flushes the code. –  Nov 30 '13 at 17:35
  • @RNI2013 I am not sure what do you mean by `flash` scanner. I see that you are only invoking its `next()` method which will read next found word (if there is any) and will not store it anywhere. I don't see purpose of such behaviour especially if it is depended of throwing exception before. – Pshemo Nov 30 '13 at 17:44
-1

Mayby you get and exception and the code enters in the catch then in finally

Tray to see if you enter in the catch block

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65