我的程序中有一个扫描仪,可以读取部分文件并将其格式化为 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() 并更改临时字符串和第二个扫描仪以扫描每个新行等。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)