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. java 五子棋之人机对战思路详解

    最近做了五子棋,记录下自己完成五子棋的人机对战的思路. 首先,思路是这样的:每当人手动下一颗棋子(黑子)的时候,应当遍历它周围棋子的情况,并赋予周围棋子一定的权值,当在机器要下棋子(白子)守护之前,会 ...

  2. 【问题】VH

      [问题]: CSS中使用了VH,在iOS中展示正常,但是在安卓的个别浏览器中,当输入框弹出时,使用VH的DIV的高度会发生变化. [原因]: 在安卓端浏览器虚拟键盘弹出时,导致视口高度改变,以至于 ...

  3. Report Studio中树提示如何使用

    环境:比如在一个销售数据里面,用户既要选择年,又要选择月,还要选择日,或者是随意选择其中的一个作为筛选条件,如果是Cube的话是可以通过拖拉不同的维度层级来实现该功能的,但是如果是FM开发的DMR模型 ...

  4. 推荐大家一个CSS书写规范

    CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等) 2.大小(width, height, padding, margin) ...

  5. 求证:a^4+b^4 ≧a^3*b+a*b^3

    证明: a4+b4-a3b-ab3 =a3(a-b)-b3(a-b) =(a3-b3)(a-b) =(a-b)2(a2+ab+b2) 而a2+ab+b2=a2+ab+b2/4+3b2/4=(a+b/2 ...

  6. 【数据压缩】LZW算法原理与源代码解析

    转载请注明出处:http://blog.csdn.net/luoshixian099/article/details/50331883 <勿在浮沙筑高台> LZW压缩算法原理很easy,因 ...

  7. Redis源代码剖析和凝视(八)--- 对象系统(redisObject)

    Redis 对象系统 1. 介绍 redis中基于双端链表.简单动态字符串(sds).字典.跳跃表.整数集合.压缩列表.高速列表等等数据结构实现了一个对象系统,而且实现了5种不同的对象,每种对象都使用 ...

  8. 点击div和某些控件之外的地方隐藏div,点击div不隐藏。对象 click和document click冲突有关问题

    帮朋友解决这个问题,我发现用以往想想像的方式来实现,貌似不太可行,所以从网上找了一些解决办法,进行优化,这篇比较详细,所以拿来备忘,另一方面也希望可以帮助需要的同学! 问题背景:jQuery事件问题! ...

  9. Windows 开发之VC++垃圾清理程序软件

    概述 本程序软件的主要实现垃圾文件清理的功能,即对指定的文件格式的临时文件或垃圾文件进行遍历.扫描.显示.删除清理等功能.在程序界面设计方面,对默认对话框重新自定义绘制,主要包括标题栏的重绘.对话框边 ...

  10. ios开发-Object-C可变参数函数

    简介 一个可变参数函数是指一个函数拥有不定的参数,即为一个函数可接收多个参数.有时我们会遇到一些算术问题需要用到,比如是计算传入参数的总和,字符串的连接或是其它操作过程,我们在 OC 里也经常使用,最 ...