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);
        }
  • 相关阅读:
    VSS使用
    Delphi简单数据库连接程序
    为表增加字段与拷贝数据到另一个表
    VSTS 使用
    Delphi实现个相似的功能界面共用一个窗体
    看代码笔记
    数据库安全管理
    函数
    【USACO】Ordered Fractions 顺序的分数
    C# 专业数据库连接配置界面
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3519201.html
Copyright © 2011-2022 走看看