gpt4 book ai didi

java - 如何使用扫描仪确定行尾?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:14:13 25 4
gpt4 key购买 nike

我的程序中有一个扫描仪,可以读取部分文件并将它们格式化为 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 - 如何使用扫描仪确定行尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9203037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com