【问题标题】:How to close a Scanner object inside a try loop without closing System.in?如何在不关闭 System.in 的情况下关闭 try 循环内的 Scanner 对象?
【发布时间】:2015-07-14 09:35:56
【问题描述】:
 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();
          } 
      }  
  }

如何关闭扫描仪对象,如果我使用 finally 关闭此对象或尝试使用资源,它也会关闭 System.in,并且我不能多次从流中读取。最好的方法是什么?

【问题讨论】:

  • 为什么要关闭它?仅仅是因为有编译器警告吗?
  • 你想要达到的目标
  • 如果您希望多次调用您的方法take1,请不要在此方法中创建扫描仪对象。在您调用take1() 方法的地方创建它,并将扫描仪对象作为参数传递给您的方法,例如take1(Scanner sc),然后在多次调用您的方法后关闭您的主要方法中的扫描仪。
  • 因为编译器警告,我想关闭它。

标签: java loops try-catch


【解决方案1】:

你应该使用这个:

finally {       
    sc.reset();
}

而不是sc.close();

【讨论】:

    【解决方案2】:

    finally 阻塞并关闭打开的资源。

    • Java finally 块是用于执行重要代码如关闭连接、流等的块
    • Java finally 块无论是否处理异常都会执行。

       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();
         }
      

    根据 Naman Gala 编辑

    检查是否发生异常然后不要关闭它让它去下一个循环util no。输入否则关闭它。

    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");
                    }
                }
    
            }
    
        }
    

    【讨论】:

    • try-catch 块在 while 循环内。
    • 假设发生了异常,那么程序的行为会是什么?
    • 是的,你是对的@NamanGala 检查编辑。感谢您的指出。
    • 终于不能用了,因为它会在第一次迭代中关闭对象(还有System.in),也就是说第一次迭代后会抛出异常。
    【解决方案3】:

    在 try 中使用变量然后关闭。例如,

     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();
              }
    
          }
    
      }
    

    这种方法可以帮助你关闭对象避免 finally 阻塞。

    【讨论】:

    • 这很糟糕。如果int i = in.nextInt();in.next(): 出现异常,则资源将永远不会关闭。
    • 测试代码,理解正确后再投反对票。在这里,如果发生任何异常,它将再次询问输入。除非给出有效输入,否则程序不会终止。
    • 如果in.next(); 出现异常,则不会像我在评论中所说的那样。
    • 对不起,但我已经测试过了,这种方法不起作用。问题不在于您如何关闭对象,而是一旦关闭对象,它也会关闭 System.in,这意味着如果迭代多次,它将引发异常,因为我们无法从关闭的输入流中读取。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 2012-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-04-27
    • 2013-05-19
    相关资源
    最近更新 更多