-1

I recently installed Eclipse on a new computer and when I tried to write this code it gave me this error. I swear that I've written this type of

Scanner keyboard = new Scanner(System.in) 

many of times in my old computer and have never gotten this error until now.

import java.util.Scanner;
public class W1M1 {

public static void main(String[] args) {
    System.out.println("Hellow World");
    Scanner keyboard = new Scanner(System.in); //'keyboard' word shows this error
    System.out.println("Please enter your name: ");
    String userInput = keyboard.nextLine();
    System.out.println("Hello there, " + userInput + ".");
}

}

1 Answers1

0

The Scanner class implements the Closable interface - i.e., it opens a resource, and you should call the close() method yourself, so, generally speaking, Eclipse does the right thing and produces a warning here.

However, in this specific case, keyboard is a Scanner that refers to System.in - the standard input descriptor. You should never close this yourself, and should just suppress Eclipse's warning in this case.

Mureinik
  • 297,002
  • 52
  • 306
  • 350