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 ...
随机推荐
- Qt 事件处理机制 (上篇)
本篇来介绍Qt 事件处理机制 .深入了解事件处理系统对于每个学习Qt人来说非常重要,可以说,Qt是以事件驱动的UI工具集. 大家熟知Signals/Slots在多线程的实现也依赖于Qt的事件处理机制. ...
- TensorFlowIO操作(一)----线程和队列
线程和队列 在使用TensorFlow进行异步计算时,队列是一种强大的机制. 为了感受一下队列,让我们来看一个简单的例子.我们先创建一个“先入先出”的队列(FIFOQueue),并将其内部所有元素初始 ...
- Vue使用中遇到问题汇总(三)
1.后台session过期前端跳转到登录页面 axios 拦截器 可以拦截请求和返回,对于失效的情况后端可以返回一个状态码,如401, 使用axios拦截时判断是401,则跳转到指定页面,如login ...
- FormBorderStyle为None的时候如何拖动窗体
//为DllImport导出命名空间, using System.Runtime.InteropServices; public partial class Form1 : System.Window ...
- [Grunt] Cleaning your build folder with grunt-contrib-clean
Grunt will clean up your build with the grunt-contrib-clean to make sure that no artifacts from prev ...
- PL/SQL 之 基础
PL/SQL(Procedural Language extensions to SQL)是Oracle 对标准 SQL 语言的过程化扩展,是专门用于各种环境下对 Oracle 数据库进行访问和开发的 ...
- linux下运行telnet命令出现command not find解决办法
原因是没有安装telnet客户端和服务(缺一不可) yum list telnet* 查看telnet相关的安装包yum install telnet-server 安装telnet服务yum i ...
- 修复错误配置/etc/fstab文件导致系统无法正常启动
1.文件介绍 /etc/fstab这个文件描述系统中各种文件系统的信息,应用程序读取这个文件,然后根据其内容进行自动挂载的工作.作为系统配置文件,fstab通常都位于/etc目录下,它包括了所有分 ...
- iOS 状态栏和导航条配置
iOS 状态栏和导航条配置 一:隐藏: [self.navigationController setNavigationBarHidden:YES animated:YES]; [[UIApplica ...
- 〖Linux〗Debian 7.1.0 Wheezy使用ltib报错的解决办法
报错内容: scue@Link:/home/work/ltib$ ./ltib Processing platform: Phytec board with the NXP LPC32XX SoC = ...