Java学习笔记之Scanner报错java.util.NoSuchElementException
转载自:IT 学习者 - 螃蟹
一个方法 A 使用了 Scanner,在里面把它关闭了。然后又在方法 B 里调用方法 A 之后就不能再用 Scanner 了 Scanner in = new Scanner(System.in);
测试代码如下:
import java.util.Scanner; /** * * @author IT 学习者 - 螃蟹 * * */ public class ItxxzScanner { //第一次输入 public void FistTime (){ Scanner sc = new Scanner (System.in); int first = sc.nextInt(); System.out.println("first:"+first); sc.close();} //第二次输入 public void SecondTime(){ Scanner sc = new Scanner (System.in); int second = sc.nextInt(); System.out.println("second:"+second); sc.close();} //测试入口 public static void main(String arg[]){ ItxxzScanner t = new ItxxzScanner(); t.FistTime(); t.SecondTime();} }
运行后便抛出如下异常:
可以看出,在代码第 29 行的时候报错,抛出了 java.util.NoSuchElementException 异常,
下面我们来分析一下报错的原因:
1、在 FistTime(){...} 使用 sc.close(); 进行关闭处理,会把 System.in 也关闭了
2、当下次在 SecondTime(){...} 方法中再进行 new Scanner (System.in) 操作读取的时候,因为输入流已经关闭,所以读取的值就是 -1;
3、在 Scanner 的 readinput 方法里面有以下代码:
try { n = source.read(buf); } catch (IOException ioe) { lastException = ioe; n = -1; }if (n == -1) {
sourceClosed = true;
needInput = false;
}
4、因为读到了 -1 就设置 sourceClosed =true;neepinput=false;
5、在 next 方法里面有以下代码:
if (needInput) readInput(); else throwFor();
6、当 needinput 为 false, 就执行 throwFor,因此再看 throwFor
skipped = false; if ((sourceClosed) && (position == buf.limit())) throw new NoSuchElementException(); else throw new InputMismatchException();
7、position 是当前读取的内容在缓冲区中位置,因为读取的是 -1, 因此 position =0, 而 buf.limit()也等于 0,因此就执行了 throw new NoSuchElementException();