英文:
Simple java spell checker / check if the word exists in the dictionary
问题
以下是您要翻译的代码部分:
import java.util.Scanner;
import java.io.File;
public class SpellChecker
{
public static void main(String[] args) throws Exception
{
Scanner write = new Scanner(System.in);
System.out.println("Type a sentence and I will check your spelling/correct words :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
for(int i = 0; i < splitSentence.length; i++)
{
Scanner read = new Scanner(new File("dictionary.txt"));
while(read.hasNextLine())
{
String compare = read.nextLine();
if(compare.equalsIgnoreCase(splitSentence[i]))
{
System.out.println(splitSentence[i] + " : correct");
}
else
{
System.out.println(splitSentence[i] + " : incorrect");
}
}
}
}
}
这是您期望的输出:
Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
my : correct
name : correct
is : correct
Ivam : incorrect
英文:
I am coding a simple spell checker. Just to check the spelling of the user text with that of a small dictionary file and compare them.
The 'dictionary.txt' file contains:
my
name
is
Here is my code to check and compare user text with the dictionary:
import java.util.Scanner;
import java.io.File;
public class SpellChecker
{
public static void main(String[] args) throws Exception
{
Scanner write = new Scanner(System.in);
System.out.println("Type a sentence and I will check your spelling/correct words :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
for(int i = 0; i < splitSentence.length; i++)
{
Scanner read = new Scanner(new File("dictionary.txt"));
while(read.hasNextLine())
{
String compare = read.nextLine();
if(compare.equalsIgnoreCase(splitSentence[i]))
{
System.out.println(splitSentence[i] + " : correct");
}
else
{
System.out.println(splitSentence[i] + " : incorrect");
}
}
}
}
}
This is the output I am getting.
Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
Heyo : incorrect
Heyo : incorrect
my : correct
my : incorrect
my : incorrect
name : incorrect
name : correct
name : incorrect
is : incorrect
is : incorrect
is : correct
Ivam : incorrect
Ivam : incorrect
Ivam : incorrect
Below is what I expected the output to be:
Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
my : correct
name : correct
is : correct
Ivam : incorrect
答案1
得分: 1
尝试这个。
public static void main(String[] args) throws Exception {
Set<String> dictionary = new HashSet<>(Files.readAllLines(Paths.get("dictionary.txt")));
java.util.Scanner write = new Scanner(System.in);
System.out.println("输入一个句子,我将检查您的拼写/正确单词:)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
for (String word : splitSentence)
if (dictionary.contains(word))
System.out.println(word + " : 正确");
else
System.out.println(word + " : 不正确");
}
输出
输入一个句子,我将检查您的拼写/正确单词:)
Heyo my name is Ivam
Heyo : 不正确
my : 正确
name : 正确
is : 正确
Ivam : 不正确
英文:
Try this.
public static void main(String[] args) throws Exception {
Set<String> dictionary = new HashSet<>(Files.readAllLines(Paths.get("dictionary.txt")));
java.util.Scanner write = new Scanner(System.in);
System.out.println("Type a sentence and I will check your spelling/correct words :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
for (String word : splitSentence)
if (dictionary.contains(word))
System.out.println(word + " : correct");
else
System.out.println(word + " : incorrect");
}
output
Type a sentence and I will check your spelling/correct words :)
Heyo my name is Ivam
Heyo : incorrect
my : correct
name : correct
is : correct
Ivam : incorrect
答案2
得分: 0
我看到的问题是,while循环将比较特定单词与字典中的每个单词,因此会返回多个结果。一个解决方案是,如果找到了正确的单词,就在控制台上打印出它是正确的,否则,只有在遍历完整个字典并找不到匹配时,才打印出它是不正确的。
在这种情况下,通过将字符串设置为不正确的默认值,并且仅在找不到匹配时才更改为正确,来解决了这个问题。然后在循环结束时只打印最新的结果字符串。我还在循环外初始化了变量,因此不必每次都执行,因为这是不必要的。
以下是解决方案的工作原理:
import java.util.Scanner;
import java.io.File;
public class SpellChecker
{
public static void main(String[] args) throws Exception
{
Scanner write = new Scanner(System.in);
System.out.println("Type a sentence and I will check your spelling/correct words :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
String result = null;
String compare = null;
Scanner read = null;
for(int i = 0; i < splitSentence.length; i++)
{
read = new Scanner(new File("dictionary.txt"));
result = "incorrect";
while(read.hasNextLine())
{
compare = read.nextLine();
if(compare.equalsIgnoreCase(splitSentence[i]))
{
result = "correct";
break;
}
}
System.out.println(splitSentence[i] + " : " + result);
}
}
}
请注意,我已经将HTML实体“"”替换为双引号,以便代码能够正确解释。
英文:
The problem as I see it is that the while loop will run compare the specific word to each word in the dictionary, therefore returning multiple results. One solution would be to, if the word is correctly found, then print out to the console that it is correct, otherwise, if and only if it has run through all of the dictionary and found no matches, then print out that it is incorrect.
In this case its solved by having the string set as default for incorrect and only change to correct if it doesnt find the match. Then at the end of the loop it just prints the latest result string. I also initialized the variables outside the loop so its not done every single time as it is unnecessary.
Here is how the solution could work:
import java.util.Scanner;
import java.io.File;
public class SpellChecker
{
public static void main(String[] args) throws Exception
{
Scanner write = new Scanner(System.in);
System.out.println("Type a sentence and I will check your spelling/correct words :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
String result = null;
String compare = null;
Scanner read = null;
for(int i = 0; i < splitSentence.length; i++)
{
read = new Scanner(new File("dictionary.txt"));
result = "incorrect";
while(read.hasNextLine())
{
compare = read.nextLine();
if(compare.equalsIgnoreCase(splitSentence[i]))
{
result = "correct";
break;
}
}
System.out.println(splitSentence[i] + " : " + result);
}
}
}
答案3
得分: 0
我已测试以下代码,并且它按预期工作。对于每个单词,每当您在字典中找到一个单词,您可以将标志设置为true并终止循环。如果在结尾处找不到单词,您可以检查此标志并打印它不正确。
import java.util.Scanner;
import java.io.File;
public class test {
public static void main(String[] args) throws Exception {
Scanner write = new Scanner(System.in);
System.out.println("输入一个句子,我将检查你的拼写/正确的单词 :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
for (int i = 0; i < splitSentence.length; i++) {
Scanner read = new Scanner(new File("dictionary.txt"));
boolean found = false;
while (read.hasNextLine()) {
String compare = read.nextLine();
if (compare.equalsIgnoreCase(splitSentence[i])) {
System.out.println(splitSentence[i] + " : 正确");
found = true;
break;
}
}
if (!found)
System.out.println(splitSentence[i] + " : 不正确");
}
}
}
如果需要进一步帮助,请随时提问。
英文:
I have tested the below code and it is working as expected. For each word whenever you find a word in dictionary you can set a flag to true and break the loop. If you dont find a word at the end you can check for this flag and print it is incorrect.
import java.util.Scanner;
import java.io.File;
public class test
{
public static void main(String[] args) throws Exception
{
Scanner write = new Scanner(System.in);
System.out.println("Type a sentence and I will check your spelling/correct words :)");
String sentence = write.nextLine();
String[] splitSentence = sentence.split(" ");
for(int i = 0; i < splitSentence.length; i++)
{
Scanner read = new Scanner(new File("dictionary.txt"));
boolean found = false;
while(read.hasNextLine())
{
String compare = read.nextLine();
if(compare.equalsIgnoreCase(splitSentence[i]))
{
System.out.println(splitSentence[i] + " : correct");
found=true;
break;
}
}
if(!found)
System.out.println(splitSentence[i] + " : incorrect");
}
}
}
答案4
得分: 0
在while
循环外部设置一个boolean
标志,并在while之外添加如下检查:
...
boolean isCorrect = false;
while(read.hasNextLine()) {
String compare = read.nextLine();
if (compare.equalsIgnoreCase(splitSentence[i])) {
isCorrect = true;
break;
}
}
if (isCorrect) {
System.out.println(splitSentence[i] + " : 正确");
} else {
System.out.println(splitSentence[i] + " : 不正确");
}
...
英文:
Have a boolean
flag outside of while
loop and add a check outside of while as below
...
boolean isCorrect = false;
while(read.hasNextLine()) {
String compare = read.nextLine();
if (compare.equalsIgnoreCase(splitSentence[i])) {
isCorrect = true;
break;
}
}
if (isCorrect) {
System.out.println(splitSentence[i] + " : correct");
} else {
System.out.println(splitSentence[i] + " : incorrect");
}
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论