如何在不关闭基础System.in的情况下关闭扫描仪? [英] How to close a scanner without closing the underlying System.in?

查看:111
本文介绍了如何在不关闭基础System.in的情况下关闭扫描仪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我关闭一个扫描仪对象并制作一个新的对象,然后尝试读取更多输入,我将得到NoSuchElementException异常.

If I close one scanner object and make a new one and try to read some more input, I'm getting the NoSuchElementException exception.

我的代码工作正常,但是如果我不关闭扫描仪,它会警告我.但是,如果我关闭它以消除警告,我也会关闭System.in ...如何避免这种情况?

My code works fine but it gives me a warning if I don't close the scanner. However, if I close it to get rid of the warning, I also close System.in... How do I avoid this?

还有,不关闭扫描仪会有什么后果吗?

Also, are there any consequences of not closing the scanner?

这是我的代码:

这是NameAddressExists()方法:

This is the NameAddressExists() method:

public void NameAddressExists() {
    System.out.println("Enter name");
    Scanner sc = new Scanner(System.in);
    String n = sc.next();
    System.out.println("Enter address");
    String a = sc.next();
    int flag = 0;
    for(int i = 0; i < count; i++) {
        if(array[i].name .equals(n) && array[i].address .equals(a)) {
            System.out.println("True");
            flag = 1;
        }
    }
    if(flag != 1) {
        new Agency(n, a);
    }
    sc.close();
}

这是PanNumberExists()方法:

This is the PanNumberExists() method:

public boolean PanNumberExists() {
    Scanner s = new Scanner(System.in);
    String n = "";
    System.out.println("Enter the 5 digits");
    try {
        n = s.nextLine();
    }catch(Exception e) {
        System.out.println(e);
    }finally {
        s.close();
    }
    if(n  .equals(this.PAN.subSequence(4,9))) {
        return true;
    }
    else {
        return false;
    }
}

这些方法是从以下main()方法调用的:

These methods are called from the following main() method:

public static void main(String args[]) {
    Agency obj1 = new Agency("XYZ", "ABCD");
    Agency obj2 = new Agency("XYZ", "ABCDEFG", "+91083226852521", "ab 1234567", "abcd12345ab");
    // Agency obj3 = new Agency("XYZ", "TSRK", "36", "ab 1234567", "abcd12345ab");
    obj1.NameAddressExists();
    System.out.println(obj2.PanNumberExists());
}

如您所见,我首先调用NameAddressExists()方法,在其中打开,使用和关闭名为'sc'的Scanner.这可以正常工作,并给我正确的输出.接下来,我调用PanNumberExists()方法,在其中打开另一个名为's'的Scanner,并尝试使用它从用户那里获取一些输入.这是我收到NoSuchElementException异常的地方.如果我在NameAddressExists()方法中将Scanner'sc'保持打开状态,则不会收到此错误.

As you can see, I first call the NameAddressExists() method, in which I open, use and close a Scanner named 'sc'. This works fine and gives me the correct output. Next, I call the PanNumberExists() method, in which I open another Scanner named 's' and try to use it to get some input from the user. This is where I receive the NoSuchElementException exception. If I leave the Scanner 'sc' open in my NameAddressExists() method, then I don't get this error.

推荐答案

Scanner scan = new Scanner(new FilterInputStream(System.in) {
    @Override
    public void close() throws IOException {
        // do nothing here ! 
    }
});

或者,通过实现自定义装饰器只需忽略close().

Or , Just ignore close() by implementing custom decorator.

public class UnClosableDecorator extends InputStream {

    private final InputStream inputStream;

    public UnClosableDecorator(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public int read() throws IOException {
        return inputStream.read();
    }

    @Override
    public int read(byte[] b) throws IOException {
        return inputStream.read(b);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return inputStream.read(b, off, len);
    }

    @Override
    public long skip(long n) throws IOException {
        return inputStream.skip(n);
    }

    @Override
    public int available() throws IOException {
        return inputStream.available();
    }

    @Override
    public synchronized void mark(int readlimit) {
        inputStream.mark(readlimit);
    }

    @Override
    public synchronized void reset() throws IOException {
        inputStream.reset();
    }

    @Override
    public boolean markSupported() {
        return inputStream.markSupported();
    }

    @Override
    public void close() throws IOException {
        //do nothing
    }
}

main()中使用时,

public static void main(String[] args) throws Exception {
        System.setIn(new UnClosableDecorator(System.in));
}

这篇关于如何在不关闭基础System.in的情况下关闭扫描仪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆