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);
}

Scanner.findInLine()与while的使用莫名其妙的导致NoSuchElementException: No line found的更多相关文章

  1. 用 Scanner 扫描CSV文件时报错:“java.util.nosuchelementexception:no line found”的解决方法

    最近用 java 对一个很大的 CSV 文件进行处理.打算用 Scanner 逐行扫描进来,结果报错 "java.util.nosuchelementexception:no line fo ...

  2. 【docker】 yaml.scanner.ScannerError: mapping values are not allowed here in "./docker-compose.yml", line 60, column 35

    在启动docker-compose 时候 报错了 命令: docker-compose up -d && docker-compose logs -f 错误代码: 解决 出现这个错误的 ...

  3. 对scanner.close方法的误解以及无法补救的错误

    scanner错误关闭导致的异常 public class test2 { public static void main(String[] args) { Scanner scanner1 = ne ...

  4. Java基础 Scanner 使用nextLine接收字符串

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  5. OCJP(1Z0-851) 模拟题分析(五)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  6. OCJP(1Z0-851) 模拟题分析(三)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  7. Day17_集合第三天

    1.HashSet类(掌握) 1.哈希值概念      哈希值:哈希值就是调用对象的hashCode()方法后返回的一个int型数字      哈希桶:简单点理解就是存储相同哈希值对象的一个容器 1. ...

  8. Kali Linux Web 渗透测试视频教程— 第七课 OpenVas

    Kali Linux Web 渗透测试视频教程— 第七课 OpenVas 文/玄魂 视频教程地址:http://edu.51cto.com/course/course_id-1887.html 目录 ...

  9. 20145222黄亚奇《Java程序设计》实验一实验报告

    实验一 Java开发环境的熟悉(Linux+Eclipse) 实验内容及步骤 使用JDK编译.运行简单的Java程序 在NetBeans IDEA中输入如下代码: package ljp; publi ...

随机推荐

  1. Linux系统getopt使用示例

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <stdint.h&g ...

  2. 程序编译是出现"field has incomplete type"问题的解决

    在编译程序是出现了如下错误, 类或结构体的前向声明只能用来定义指针对象或引用,因为编译到这里时还没有发现定义,不知道该类或者结构的内部成员,没有办法具体的构造一个对象,所以会报错. 将类成员改成指针就 ...

  3. python爬虫——利用BeautifulSoup4爬取糗事百科的段子

    import requests from bs4 import BeautifulSoup as bs #获取单个页面的源代码网页 def gethtml(pagenum): url = 'http: ...

  4. 如何查看ESXi的网卡的MAC地址?

    直接上图 图一, 物理网卡   图二, vmkernel虚拟网卡   参考资料 ============================ How To Determine Vmkernel Inter ...

  5. JS中字符串的相关操作

    一.字符串的创建 创建一个字符串有几种方法.最简单的是用引号将一组字符包含起来,可以将其赋值给一个字符串变量. var myStr = "Hello, String!"; 可以用双 ...

  6. Javascript-history.go()和history.back()的用法和区别

    简单的说就是:go(-1): 返回上一页,原页面表单中的内容会丢失:back(): 返回上一页,原页表表单中的内容会保留. history.go(-1):后退+刷新 history.back():后退 ...

  7. token 案例,只是测试,功能并不完善(只是看看token 到底是何方神圣)

    token 简单理解就是 加密 解密的一个过程 JavaWebToken(加密解密工具) public class JavaWebToken { private static Logger log = ...

  8. /etc/ssh/sshd_config 配置(ssh连接)

    # Package generated configuration file# See the sshd_config(5) manpage for details # What ports, IPs ...

  9. 【pyhon】黑客用字典暴力破解Zip文件密码原理性展示

    基本原理:用程序把字典文件里拟定好的密码一个个提取出来,去测试zip能否打开 字典文件pass.txt内容: 1224 2121 asdf abcd1234 dwsdsd dssds 程序代码: # ...

  10. Ajax datatype:'JSON'的error问题Status1:200,JSON格式

    转自:http://blog.sina.com.cn/s/blog_6e001be701017rux.html <script src="../js/jquery-1.8.0-vsdo ...