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 ...
随机推荐
- iOS:UI简单的总结
UI简单总结: 一.常用单例: NSBundle *bundel = [NSBundle mainBundle]; //加载资源 NSFileManager *fm = [NSFileManager ...
- 数学图形(2.6)Satellit curve
这曲线有点像鼓,绕在球上两头是开口的. #http://www.mathcurve.com/courbes3d/satellite/satellite.shtml vertices = t = to ...
- Resin install document
Centos6快速安装文档 resin3.1.13 软件下载地址: http://caucho.com/products/resin/download/gpl#download #系统环境[root@ ...
- .net平台推送ios消息
1,ios应用程序中允许向客户推送消息 2,需要有苹果的证书以及密码(怎么获取,网上搜一下,需要交费的) 3,iphone手机一部,安装了该ios应用程序 4,.net 项目中引用PushSharp. ...
- Node.js:Web模块、文件系统
一.web模块 Web服务器一般指网站服务器,是指驻留于因特网上某种类型计算机的程序,Web服务器的基本功能就是提供Web信息浏览服务.它只需支持HTTP协议.HTML文档格式及URL,与客户端的网络 ...
- perforce 使用教程(zz)
http://www.perforce.com/documentation/perforce_technical_documentation http://blog.csdn.net/brucexu1 ...
- VMWARE虚拟机安装64位系统此主机支持IntelVTx 但IntelVTx处于禁用状态
1.进入BIOS(我的电脑是Thinkpad e480,进入按钮是F12/ Fn+F12) 2.选择App Menu,再选择第一项Setup,进入 3.选择Security,选择下面第四项Virtua ...
- (笔试题)N!尾部连续0的个数
题目: 对任意输入的正整数N,编写C程序求N!的尾部连续0的个数,并指出计算复杂度.如:18!=6402373705728000,尾部连续0的个数是3. (不用考虑数值超出计算机整数界限的问题) 思路 ...
- T-SQL 之 视图
视图实际上就是一个存储查询,重点是可以筛选.组合和匹配来自基本表(或者其他视图)的数据,从而创建在很多方面像另一个基表那样起作用的对象.可以创建一个简单的查询,仅仅从一个表中选择几列,而忽略其他列:或 ...
- git:could not open a connection to your authentication agent
git:could not open a connection to your authentication agent 错误: vagrant@homestead:~/Code/sample$ ...