检查你的单词与文本文件 java 中的单词
我想为一个项目做一些东西,我想要它做的是你输入一个单词,它会根据文本文件中的列表检查你的单词,以确保它是一个真实的单词。
public void wordCheck() {
char[] bC = {char15, char11, char12, char13, char14};
char d;
char b;
File text = new File("text.txt");
try (BufferedReader br = new BufferedReader(new FileReader(text))) {
String line = br.readLine();
while (line != null) {
line = br.readLine();
for(int i = 0; i < 5; i++){
d = line.toCharArray()[i];
for(int j = 0; j < 5; j++) {
b = bC[j];
if(d == b) {
isReal[i] += 1;
} else {
line = "";
line = br.readLine();
for(int k = 0; k < 5; k++) {
isReal[k] = 0;
}
}
}
}
checkCheck();
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这就是我的想法,我将尝试解释它以及它的问题所在。 bC
是您输入的单词,每个字母都是字符。我有一个缓冲读取器,它读取第一行,并将其放入名为 line 的字符串中。然后,我执行检查部分,该部分基本上将行转换为字符数组,并相互检查每个字母以检查它们是否相同。如果字母相同(d == b),则 int 数组将被更改(如果第一个字母正确则为 10000,如果全部正确则为 11111,如果没有则为 00000,依此类推)。如果至少有一个字母不正确,则它将 int 数组重置回全 0。一旦完成,checkCheck
将被调用,它只是打印出 int 数组以查看它是否有效,然后读者将转到下一行。
所有这一切的问题是它不起作用,它似乎没有阅读每一行或正确检查单词。我什至不知道这是否是一个好的解决方案。
I wanted to make something for a project, and what I wanted it to do was you input a word, and it will check your word against a list in a text file to make sure that it is a real word.
public void wordCheck() {
char[] bC = {char15, char11, char12, char13, char14};
char d;
char b;
File text = new File("text.txt");
try (BufferedReader br = new BufferedReader(new FileReader(text))) {
String line = br.readLine();
while (line != null) {
line = br.readLine();
for(int i = 0; i < 5; i++){
d = line.toCharArray()[i];
for(int j = 0; j < 5; j++) {
b = bC[j];
if(d == b) {
isReal[i] += 1;
} else {
line = "";
line = br.readLine();
for(int k = 0; k < 5; k++) {
isReal[k] = 0;
}
}
}
}
checkCheck();
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
This is what I came up with and I'm going to try to explain it and what's wrong with it. bC
is the word you put in with each letter being the char's. I have a buffered reader that reads the first line, and puts it into a string called line. I then do the check part which basically turns the line into a character array, and checks each letter against each other to check if they are the same. If a letter is the same (d == b), then an int array will be changed (if the first letter is correct then it will be 10000, if all correct then 11111, if none then 00000, and so on). If at least one letter is incorrect then it resets the int array back to being all 0's. Once this is done checkCheck
will be called where it just prints out the int array to see if it worked, then the reader will go to the next line.
The issue with all this is that it doesn't work, it doesn't seem to read each line or properly check the words. I don't even know if this is a good solution or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论