java中结束scanner_java – 如何用Scanner确定一行的结尾?

我的程序中有一个扫描程序,它读取部分文件并将其格式化为HTML.当我正在阅读我的文件时,我需要知道如何让扫描仪知道它在一行的末尾并开始写入下一行.

这是我的代码的相关部分,如果我遗漏了任何内容,请告诉我:

//scanner object to read the input file

Scanner sc = new Scanner(file);

//filewriter object for writing to the output file

FileWriter fWrite = new FileWriter(outFile);

//Reads in the input file 1 word at a time and decides how to

add it to the output file

while (sc.hasNext() == true)

{

String tempString = sc.next();

if (colorMap.containsKey(tempString) == true)

{

String word = tempString;

String color = colorMap.get(word);

String codeOut = colorize(word, color);

fWrite.write(codeOut + " ");

}

else

{

fWrite.write(tempString + " ");

}

}

//closes the files

reader.close();

fWrite.close();

sc.close();

我发现了sc.nextLine(),但我仍然不知道如何确定我何时在一行结束.

解决方法:

如果您只想使用Scanner,则需要创建一个临时字符串,将其实例化为数据网格的nextLine()(因此它只返回它跳过的行)和一个扫描临时字符串的新Scanner对象.这样你只使用那一行,并且hasNext()不会返回误报(这不是一个误报,因为这是它的意图,但在你的情况下,它在技术上是).您只需将nextLine()保留在第一个扫描仪并更改临时字符串,然后使用第二个扫描仪扫描每个新行等.

标签:java-util-scanner,java,delimiter

来源: https://codeday.me/bug/20191008/1870382.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值