1

See the code below, in this code I want to declare the Scanner class instance again and again for every input instance, I know I can declare the Scanner class instance outside the loop and the problem will be solved. But I am only showing you how I am implementing the same concept some where else. I cannot write that code here that's why I want you people top give me solution by understanding my perspective.

import java.util.Scanner;

public class TestScanner {
    public static void main(String[] args) {
        int a[] = new int[4];

        System.out.println("Enter elements in array: ");

        for (int i = 0; i < a.length; i++) {
            Scanner scanner = new Scanner(System.in);
            a[i] = scanner.nextInt();
            scanner.close();
        }

        System.out.println("The Arrays is : ");

        for (int i : a) {
            System.out.println(i + "  ");
        }
    }
}
Prudhvi
  • 2,276
  • 7
  • 34
  • 54
  • 4
    The problem is that when you close the scanner, it also closes the underlying stream, in your case `System.in`. Thus you're unable to take a new input. – Alexis C. Jul 22 '15 at 19:16
  • @Alexis C so should i not close the scanner close at all ?? –  Jul 22 '15 at 19:57
  • If you have a question about an exception, then please add that exception (+ stacktrace) to the question. – Tom Jul 22 '15 at 20:18

2 Answers2

2

Remove the line scanner.close(); because it disables taking in new input through System.in. You will still need scanner in each iteration of the for loop.

deezy
  • 1,480
  • 1
  • 11
  • 22
  • but then a warning is always left that you need to close the Scanner class, i want to get rid of that. –  Jul 22 '15 at 19:55
  • Sometimes it is okay to not close it and ignore the warning. It would depend on your situation. http://stackoverflow.com/questions/5919143/is-it-safe-not-to-close-a-java-scanner-provided-i-close-the-underlying-readable – deezy Jul 22 '15 at 19:59
  • okay i will use your suggestion and thanks for your contribution –  Jul 22 '15 at 20:05
0

You can modify your code like this. You can reuse the scanner object inside your loop, no need to create one per iteration.

Scanner scanner;
for (int i = 0; i < a.length; i++) {
   scanner = new Scanner(System.in);
   a[i] = scanner.nextInt();
}
scanner.close();
Jos
  • 2,015
  • 1
  • 17
  • 22
  • 2
    Afaik, the `System.in` is automatically closed by the JVM, so closing the scanner in unnecessary in this case (Please correct me if I'm wrong). – Carcigenicate Jul 22 '15 at 19:35
  • I guess you are right. Its optional to close a `System.in`. – Jos Jul 22 '15 at 19:44
  • @redflar3 you didn't understood the scenario –  Jul 22 '15 at 19:56
  • oops.. my bad, didn't read the post fully... in your case you can just ignoring the closing of `System.in`. – Jos Jul 22 '15 at 20:02
  • @redflar3 but that causes a warning to be left in IDE, cant that be cleared –  Jul 22 '15 at 20:03
  • you can suppress the warning using annotation.. make sure to give it with the lowest scope; also, When ever you do that, you can make sure to give proper comments/documentation on why you are sure to suppress the warning (as a good practice). – Jos Jul 22 '15 at 20:08
  • 1
    @KrishanAggarwal add `@SuppressWarning("resource")` above the method, or the scanner declaration. This is one of the rare time where ignoring a warning is OK. – Carcigenicate Jul 22 '15 at 20:18
  • 1
    @KrishanAggarwal have you gone thru http://stackoverflow.com/questions/25506240/how-to-close-a-scanner-without-closing-the-underlying-system-in – Jos Jul 22 '15 at 20:21
  • Also is any possibility of dependency injection in your scenario? – Jos Jul 22 '15 at 20:25