-1

e.g)

public void a(){
     Scanner input = new Scanner(System.in);
     String a = input.nextLine();
     ....
     input.close();
}

public void b(){
     Scanner scan = new Scanner(System.in);
     int b = scan.nextInt();
     ....
     scan.close();
}

This Scanner does not operate correctly. But after remove close(), it works correctly.

I guess the reason is the 'System.in' have a problem.

Please give me answer.

Thank you

+) First call input.nextLine() in a(), and then call scan.nextInt() in b(). I have the error such as NoSuchElementException.

chulphan
  • 54
  • 7
  • *Attempting to perform search operations after a scanner has been closed will result in an IllegalStateException.* Is that what you get? – Naman Sep 13 '17 at 07:35

1 Answers1

0
public void b(){
     Scanner scan = new Scanner(System.in);
     int b = scan.nextLine();
     ....
     input.close();
}

this should not be input.close();! b() method cannot access the input variable of a() method. So you should replace it with scan.close()

public void b(){
     Scanner scan = new Scanner(System.in);
     int b = scan.nextLine();
     ....
     scan.close();
}
pleft
  • 5,282
  • 2
  • 13
  • 36