title: Using scanner in multiple classes
body: <p>I just started learning Java and wrote this code to practice creating interface. I realize that the use of the interface relation here is probably not how it's supposed to be actually used but I was doing this only for practice and basic understanding. </p>
  
  <p>So anyways, I created the program to take user input on what operation they'd like to perform and then carry out said operation. For this purpose, the user provides two inputs: first the operation that they'd like performed and then a number on which they would like it performed. So for example if they would like to find the square root of 4 they'd first enter 2 to select square root and then 4 to indicate they'd like to find the square root of 4. So for this purpose, I opened the scanner class on multiple classes. However, closing the scanner in any one of the classes creates an error because apparently once you close it in a file it cannot be reopened. I searched a lot and most suggestions were centered around saying that using multiple instances of scanner like this just creates problems so create only one. While I can rewrite this code to make that adjustment, I was wondering if there was a way that I could do what I have done without creating any warning messages like they pop up in this code:</p>
  
  <pre><code>import java.util.Scanner;
  
  public class Operation 
  {
      public static void main(String[] args)
      {
          Scanner in = new Scanner(System.in);
          System.out.println("1. Square");
          System.out.println("2. Square Root");
          System.out.print("Enter Number: ");
          int choice = in.nextInt();
  
          switch(choice)
          {
          case 1:
          Squaring s1 = new Squaring();
          s1.getDim();
          s1.operate();
          break;
  
          case 2:
          Root r1 = new Root();
          r1.getDim();
          r1.operate();
          }
      }
  }
  
  interface Method
  {
      void getDim();
      void operate();
  }
  
  class Squaring implements Method
  {
      double n, r;
  
      public void getDim()
      {
          Scanner in = new Scanner(System.in);
          System.out.print("Input Number: ");
          n = in.nextDouble();
      }
  
      public void operate()
      {
          r = n*n;
          System.out.println("Squared of " + n + " is = " + r);
      }
  }
  
  class Root implements Method
  {
      double n, r;
  
      public void getDim()
      {
          Scanner in = new Scanner(System.in);
          System.out.print("Input Number: ");
          n = in.nextDouble();
          in.close();
      }
  
      public void operate()
      {
          r = Math.sqrt(n);
          System.out.println("Square root of " + n + " is = " + r);
      }
  }
  </code></pre>