对于将文本文件字典解析为二次探测哈希表的拼写检查程序,我无法计算行号...
input = new Scanner(userInput);
int counter = 1;
while (input.hasNext())
{
String x = input.next();
x = x.replaceAll("[^a-zA-Z\\s]", "").replaceAll("\\s+", " ");
if (!dictionary.contains(x))
{
System.out.println("Mispelled word: " + x + ", on line #: " + counter);
}
if (x.contains("\n"))
counter++;
}
这可以正确识别构造为userInput的文件中任何位置的拼写错误的单词,但不会生成准确的行号。
由于输入文本文件每行包含多个单词,因此我使用input.next()方法而不是input.nextLine()。我正在尝试使用int计数器来计算正确的行号。
包含以下内容的文本文件:
This is a missspelled word test documentt.
How many dogs can fly? It doesn't produce correct linee numbbers.
Do you think this really works?
使用错误的行号生成输出:
Mispelled word: missspelled, on line #: 1
Mispelled word: documentt, on line #: 1
Mispelled word: linee, on line #: 1
Mispelled word: numbbers, on line #: 1
我使用计数器++的if语句没有做到这一点。
答案 0 :(得分:0)
这应该有效!
Scanner inputLine;
Scanner inputToken;
try {
int counter = 1;
inputLine = new Scanner(new File("a.txt"));
while(inputLine.hasNextLine()){
inputToken=new Scanner(inputLine.nextLine());
while (inputToken.hasNext())
{
String x = inputToken.next();
x = x.replaceAll("[^a-zA-Z\\s]", "").replaceAll("\\s+", " ");
if (!dictionary.contains(x))
{
System.out.println("Mispelled word: " + x + ", on line #: " + counter);
}
if (x.contains("\n"))
counter++;
}
counter++;
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}