扫描仪中的连接未关闭

时间:2016-02-29 05:11:33

标签: java

我得到了输入文件名的命令行参数,我已经使用扫描仪连接来读取该文本文件的输入,并且在执行操作后,我关闭了扫描仪连接。即使它显示了NZEC(运行时错误) )。

在我的机器中,我正在使用较少的输入,因此错误没有重现,但在线编程竞赛网站中出现错误

Sol:如果我使用try-Catch,问题就解决了。

但请告诉我这个错误的原因

Whats_next()
 {
    Scanner s=new Scanner(System.in);
    while(s.hasNextLine())
    {
    int x=s.nextInt();
    int y=s.nextInt();
    int z=s.nextInt();
    if(x!=0 || y!=0 || z!=0)
    {
        if((y-x)==(z-y))
            {
                System.out.print("AP");
                System.out.println("\t"+(z+(y-x)));
            }
        else if((y/x)==(z/y))
            {
                System.out.print("GP");
                System.out.println("\t"+z*(y/x));
            }
    }
    }
    s.close();
 }


    public static void main(String[] args) 
    {
    try
    {
        if(args.length==1)
            System.setIn(new FileInputStream(args[0]));
    }
    catch(Exception e)
    {

    }
        Whats_next w=new Whats_next();
    }

1 个答案:

答案 0 :(得分:0)

您应该关闭finally块中的连接。

try {
    Scanner scanner = new Scanner(System.in);
    // your logic
}
finally {
    if(scanner!=null)
        scanner.close();
}
相关问题