0

I have this code that works but I cannot close my scanner after I'm done. scanner.close() does not work anywhere and using try(Scanner scaner etc. does not seem to work either. Can anyone tell me how to close a scanner in a code like mine?

import java.util.Random;
import java.util.Scanner;

public class GuessingGame {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Random randomGenerator = new Random();
        int input;
        int code = 0;
        int i = 0;
        int[] guesses = new int[7];

        System.out.println("Secretly type the code or input -1 if you want me to                              choose");
        input = scanner.nextInt();
        if (input == -1) {
            code = randomGenerator.nextInt(100);
        }
        else {
            code = input;
        }

        System.out.println("Start guessing!");
        while (i < 7) {
            guesses[i] = scanner.nextInt();

            if (guesses[i] == code) {
                System.out.println("Good guess! You won.");
                System.out.println((i+1) +" guesses");
                i++;



                for (int k=0; k<i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");
                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }

            else if (code < guesses[i] && i != 6) {
                System.out.println("lower");
                i++;
            }

            else if (code > guesses[i] && i != 6) {
                System.out.println("higher");
                i++;
            }

            else {

                System.out.println("No more guesses, you lost");
                System.out.println((i+1) + " guesses");


                for (int k=0; k<=i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");


                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }
        }
    }
} 
mkobit
  • 43,979
  • 12
  • 156
  • 150
Jeroen
  • 1
  • 1
  • 1
  • 6
    Leaving aside the fact that closing `System.in` is not a good idea, why do you think `scanner.close()` does not work anywhere? – Codebender Sep 14 '15 at 16:34
  • 1
    Here's a helpful question & answer. http://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in – Ben M. Sep 14 '15 at 16:37

4 Answers4

1

When you wrap a stream with another (like you do in scanner) closing the stream closes the wrapped streams.

That means you would close System.in if you closed your scanner.

I recommend setting your scanner variable to null, and letting the garbage collector remove it from the heap. Unless you explicitly want to close the input to the program, this will likely have the desired effect.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
0

You should close the scanner after the while loop. Else you will certainly get errors.

Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45
0

use

Scanner scanner = ....;
try {
 while () {} ....
} catch (Exception ex) { 
 try {scanner.close();}catch {}  // closes the scanner in case of an exception
} finally { try {scanner.close(); } catch {}}  // makes sure that the scanner closes. try catch because it may fail.
Emanuel
  • 8,027
  • 2
  • 37
  • 56
0

I cant reproduce your error with the scanner.close() method but I think it is not working inside a loop. Here is an example with it working for me:

import java.util.Random;
import java.util.Scanner;



public class Test{


public static void main(String[] args) {
     Scanner scanner = new Scanner(System.in);
        Random randomGenerator = new Random();
        int input;
        int code = 0;
        int i = 0;
        int[] guesses = new int[7];

        System.out.println("Secretly type the code or input -1 if you want me to                              choose");
        input = scanner.nextInt();
        if (input == -1) {
            code = randomGenerator.nextInt(100);
        }
        else {
            code = input;
        }

        System.out.println("Start guessing!");
        while (i < 7) {
            guesses[i] = scanner.nextInt();

            if (guesses[i] == code) {
                System.out.println("Good guess! You won.");
                System.out.println((i+1) +" guesses");
                i++;



                for (int k=0; k<i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");
                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }

            else if (code < guesses[i] && i != 6) {
                System.out.println("lower");
                i++;
            }

            else if (code > guesses[i] && i != 6) {
                System.out.println("higher");
                i++;
            }

            else {

                System.out.println("No more guesses, you lost");
                System.out.println((i+1) + " guesses");


                for (int k=0; k<=i; k++) {
                    for (int j=0; j<100; j++)
                    {

                        if (j == guesses[k]) {
                            System.out.print("X");


                        }

                        else if (j == code) {
                            System.out.print("|");
                        }

                        else {
                            System.out.print(".");
                        }
                    }

                    System.out.println("");
                }

            }
        }


        scanner.close();

}
}