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);
}
Scanner.findInLine()与while的使用莫名其妙的导致NoSuchElementException: No line found的更多相关文章
- 用 Scanner 扫描CSV文件时报错:“java.util.nosuchelementexception:no line found”的解决方法
最近用 java 对一个很大的 CSV 文件进行处理.打算用 Scanner 逐行扫描进来,结果报错 "java.util.nosuchelementexception:no line fo ...
- 【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 错误代码: 解决 出现这个错误的 ...
- 对scanner.close方法的误解以及无法补救的错误
scanner错误关闭导致的异常 public class test2 { public static void main(String[] args) { Scanner scanner1 = ne ...
- Java基础 Scanner 使用nextLine接收字符串
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- OCJP(1Z0-851) 模拟题分析(五)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- OCJP(1Z0-851) 模拟题分析(三)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- Day17_集合第三天
1.HashSet类(掌握) 1.哈希值概念 哈希值:哈希值就是调用对象的hashCode()方法后返回的一个int型数字 哈希桶:简单点理解就是存储相同哈希值对象的一个容器 1. ...
- Kali Linux Web 渗透测试视频教程— 第七课 OpenVas
Kali Linux Web 渗透测试视频教程— 第七课 OpenVas 文/玄魂 视频教程地址:http://edu.51cto.com/course/course_id-1887.html 目录 ...
- 20145222黄亚奇《Java程序设计》实验一实验报告
实验一 Java开发环境的熟悉(Linux+Eclipse) 实验内容及步骤 使用JDK编译.运行简单的Java程序 在NetBeans IDEA中输入如下代码: package ljp; publi ...
随机推荐
- 数学图形(2.18)Hyperbolical conical spiral双曲圆锥螺线
双曲圆锥螺线 #http://www.mathcurve.com/courbes3d/spiralehyperbolique/spiralehyperbolique.shtml vertices = ...
- 数学图形(1.29) cochleoid曲线
它也算是一种螺线吧 相关软件参见:数学图形可视化工具,使用自己定义语法的脚本代码生成数学图形.该软件免费开源.QQ交流群: 367752815 #http://www.mathcurve.com/co ...
- myBatis + SpringMVC上传、下载文件
摘自: http://limingnihao.iteye.com/blog/1069503 环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何 ...
- Thinkphp学习笔记3-前置和后置操作
前置和后置操作指的是在执行某个操作方法之前和之后会自动调用的方法,不过仅对访问控制器有效. 其他的分层控制器层和内部调用控制器的情况下前置和后置操作是无效的. 系统会检测当前操作是否具有前置和后置操作 ...
- Asp.net 在网页编写C#代码示例-- 一个简单的web MsSql 命令执行环境
在给一个客户做的系统上,因为要对数据库进行查看,但之前都是用TeamView来连接到客户的服务器进行数据库操作的 但最近客户那边的TeamView好像更改过密码导致我无法正常连接,而巧了客户的网官因为 ...
- windows live writer首行缩进问题的解决
使用live writer写博客的确方便,但有个简单的问题,我始终无法解决,就是发布的博客老是无法首行缩进,试过好多方法,都有问题: 直接加全角空格.上传时就给过滤掉了. 修改defaultcss,结 ...
- 超棒的HTML5/CSS3单页面响应式模板(支持Bootstrap)
在线演示 使用Bootstrap实现的响应式单页面模板. 桌面效果: 移动设备效果: 阅读全文:超棒的HTML5/CSS3单页面响应式模板(支持Bootstrap)
- ExplorerControls的显示问题
我们都知道ArcGIS桌面版本"添加数据"的功能是弹出一选择对话框,如下图所示,但我们总想搞自己的,以便融入自己的风格.下图左边是ArcGIS自带的"添加数据" ...
- ReSharper的功能真的很强大主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚
二.ReSHarper 代码规范.单元测试.... ReSharper的功能真的很强大,主要是针对代码规范和优化,园子里介绍的也不少,如果你没有安装,那我只能表示你们会相见恨晚! 1.像命名不规范,f ...
- QtGui.QGridLayout
The most universal layout class is the grid layout. This layout divides the space into rows and colu ...