-1
 public static int take1(){
      Scanner in = new Scanner(System.in);
      while(true){
          try{
              System.out.print("\nHow many coloms you want = ");
              return in.nextInt();
          }
          catch(Exception e ){
              System.out.println("Sorry,Please enter only no.");
              in.next();
          } 
      }  
  }

how to close the scanner object , if i close this object using finally or try with resources it also closes System.in , and i cant read from stream more than once. what is the best method to do this?

mightyWOZ
  • 7,946
  • 3
  • 29
  • 46
  • 1
    Why do you want to close it? Is it just because there is a compiler warning? – Codebender Jul 14 '15 at 09:36
  • 1
    what you want to achieve – kirti Jul 14 '15 at 09:38
  • 4
    possible duplicate of [Close Scanner without closing System.in](http://stackoverflow.com/questions/14962082/close-scanner-without-closing-system-in) – mhlz Jul 14 '15 at 09:52
  • If you are looking to call your method `take1` multiple times, don't create scanner object in this method. Create it where you are calling the `take1()` method and pass the scanner object as argument to your method, like `take1(Scanner sc)`, then close the scanner in your main method once you are done calling your method multiple times. – Amit.rk3 Jul 14 '15 at 09:59
  • I want to close it because of compiler warning. – mightyWOZ Jul 14 '15 at 10:06
  • Thanks @Amit.rk3 , your approach worked for me , atleast now i dont see that compiler warning. – mightyWOZ Jul 14 '15 at 10:23

3 Answers3

1

You should use this:

finally {       
    sc.reset();
}

rather than sc.close();

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
0

put finally block and close the open resource.

  • Java finally block is a block that is used to execute important code such as closing connection, stream etc.
  • Java finally block is always executed whether exception is handled or not.

     try
      {
          System.out.print("\nHow many coloms you want = ");
          return in.nextInt();
      } catch(Exception e ){
                System.out.println("Sorry,Please enter only no.");
                in.next();
       }finally{
                in.close();
       }
    

EDITED AS PER Naman Gala

check if exception occured then don't close it let it go for next loop util no. is entered otherwise close it.

public static int take1()
    {
        boolean isExceptionOccured=false;
        Scanner in = new Scanner(System.in);
        while(true)
        {
            try
            {
                System.out.print("\nHow many coloms you want = ");
                isExceptionOccured=false;
                return in.nextInt();
            }
            catch(Exception e )
            {
                isExceptionOccured=true;
                System.out.println("Sorry,Please enter only no.");
                in.next();
            }finally{
                if(!isExceptionOccured){
                in.close();
                System.out.println("resource closed");
                }
            }

        }

    }
Rustam
  • 6,485
  • 1
  • 25
  • 25
-3

Use a variable inside try and then close. For example,

 public static int take1()
  {
      Scanner in = new Scanner(System.in);
      while(true)
      {
          try
          {
              System.out.print("\nHow many coloms you want = ");
              int i =  in.nextInt();
              in.close();
              return i;
          }
          catch(Exception e )
          {
              System.out.println("Sorry,Please enter only no.");
              in.next();
          }

      }

  }

This approach help you to close the object avoiding finally block.

  • 1
    This is bad. If there's an exception at `int i = in.nextInt();` or at `in.next():` the resource will never get closed. – mhlz Jul 14 '15 at 09:51
  • Test the code and then give down vote after proper understanding. Here if any exception occurs it will again asks the input. The program will not terminated unless a valid input is given. – Mohammad Mosiur Jul 14 '15 at 09:53
  • 1
    Not if there's an exception at `in.next();` like I said in my comment. – mhlz Jul 14 '15 at 09:55
  • Sorry , but i have tested it already and this approach does not work. the problem is not how you close the object , but once the object is closed it also closes System.in , which means if iterate for more than once ,it will throw an exception since we can not read from the closed input stream. – mightyWOZ Jul 14 '15 at 10:15