如果我关闭一个扫描仪对象并创建一个新对象并尝试阅读更多输入,我将获得NoSuchElementException
例外。
我的代码工作正常,但如果我不关闭扫描仪,它会给我一个警告。但是,如果我关闭它以消除警告,我也会关闭System.in
...我该如何避免这种情况?
另外,没有关闭扫描仪会有什么后果吗?
编辑:这是我的代码:
这是NameAddressExists()方法:
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()方法:
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()方法调用这些方法:
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()方法,在该方法中,我打开,使用并关闭名为&#39; sc的#{1}}。这很好,并给我正确的输出。接下来,我调用PanNumberExists()方法,在其中打开另一个Scanner
命名的&#39; s&#39;并尝试使用它来获取用户的一些输入。这是我收到Scanner
例外的地方。如果我离开NoSuchElementException
&#39; sc&#39;在我的NameAddressExists()方法中打开,然后我就不会收到此错误。
答案 0 :(得分:2)
Scanner scan = new Scanner(new FilterInputStream(System.in) {
@Override
public void close() throws IOException {
// do nothing here !
}
});
或者,只需通过实现自定义装饰器来忽略close()
。
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));
}
答案 1 :(得分:0)
您可以使用装饰器模式并创建无法关闭的自定义InputStream
,然后将其传递给Scanner
import java.io.IOException;
import java.io.InputStream;
public class PreventClosingInputStream extends InputStream {
private InputStream inputStream;
public PreventClosingInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public int read() throws IOException {
return inputStream.read();
}
@Override
public void close() throws IOException {
// Don't call super.close();
}
}
然后,在您的代码中:
PreventClosingInputStream in = new PreventClosingInputStream(System.in);
Scanner s = new Scanner(in);
// ...
s.close(); // This will never close System.in as there is underlying PreventClosingInputStream with empty close() method
使用try-with-resources:
try (PreventClosingInputStream in = new PreventClosingInputStream(System.in);
Scanner s = new Scanner(in);) {
// ...
// resources will be automatically closed except of System.in
}