public static boolean parseHTML(Scanner sc, List<String> errorInfo) { String[] tags = new String[DEFAULT_CAPACITY]; int count = 0; // tag counter String token; // token returned by the scanner while (sc.hasNextLine()) { while ((token = sc.findInLine("<[^>]*>"))!=null) { // find the next tag: starting with a '<', ending with a '>', anything between them are recognized as tag name. Note that '<>' is also taken into account. if (count == tags.length) { tags = Arrays.copyOf(tags, tags.length * 2); } tags[count++] = stripEnds(token); // strip the ends off this tag } sc.nextLine(); } return isHTMLMatched(tags, errorInfo); }
症状:
while(sc.hasNextLine())通过,开始处理最后一行文本,运行到sc.nextLine()的时候,抛出异常:NoSuchElementException: No line found
经过println(sc.hasNextLine())的检查,在进入while((toke = sc.findInLine("...")) != null)之前,打印true,跳出该循环之后,打印false
分析:
查看findInLine的API Specification,没有提及findInLine()具有nextLine()的功能。
暂时不明究竟是什么原因导致了这个问题。
解决方案:改写while,如下
public static boolean parseHTML(Scanner sc, List<String> errorInfo) { String[] tags = new String[DEFAULT_CAPACITY]; int count = 0; // tag counter String token; // token returned by the scanner while (true) { while ((token = sc.findInLine("<[^>]*>"))!=null) { // find the next tag: starting with a '<', ending with a '>', anything between them are recognized as tag name. Note that '<>' is also taken into account, but it's illegal. if (count == tags.length) { tags = Arrays.copyOf(tags, tags.length * 2); } tags[count++] = stripEnds(token); // strip the ends off this tag } if (sc.hasNextLine()) { sc.nextLine(); } else { break; } } return isHTMLMatched(tags, errorInfo); }