zoukankan      html  css  js  c++  java
  • Scanner.findInLine()与while的使用莫名其妙的导致NoSuchElementException: No line found

    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);
        }
  • 相关阅读:
    Eclipse快捷键大全(转载)
    IE9浏览Flash页面时显示错位并不停地闪烁
    flash全屏事件和键盘按下事件部分不能触发问题
    AS3摘要(转载)
    【as3手册小记】ActionScript 中处理全屏模式的注意事项
    巧用FlashPaper 让Word文档变Flash
    AS3视频照相截图(转载)
    Json串到json对象的转换
    映射文件详解(转)
    Jquery .ajax方法分析(一)
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3519201.html
Copyright © 2011-2022 走看看