Jeroen
Jeroen

Reputation: 1

How do I close my scanner?

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("");
                }

            }
        }
    }
} 

Upvotes: 0

Views: 7418

Answers (4)

Manuel Prinz
Manuel Prinz

Reputation: 45

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();

}
}

Upvotes: 0

Emanuel
Emanuel

Reputation: 8106

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.

Upvotes: 0

Edwin Buck
Edwin Buck

Reputation: 70929

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.

Upvotes: 1

Bruno Caceiro
Bruno Caceiro

Reputation: 7199

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

Upvotes: 0

Related Questions