gpt4 book ai didi

java - 找出文件中与字符串数组中的任何单词都不匹配的单词数

转载 作者:行者123 更新时间:2023-11-29 03:08:23 25 4
gpt4 key购买 nike

我正在尝试创建一个从文件中读取数据的程序。我希望每次都检查文件中的下一个单词是否与特定字符串数组中的特定单词匹配。

每次单词不匹配时,我都想记录(错误++)的次数,并打印文件中的单词与字符串数组中的至少一个单词不匹配的次数。

这是我的程序:

public class main_class {

public static int num_wrong;
public static java.io.File file = new java.io.File("text.txt");
public static String[] valid_letters = new String[130];
public static boolean wrong = true;
public static String[] sample = new String[190];

public static void text_file() throws Exception {
// Create Scanner to read file

Scanner input = new Scanner(file);

String[] valid_letters = { "I", " have ", " got ", "a", "date", "at",
"quarter", "to", "eight", "8", "7:45", "I’ll", "see", "you",
"the", "gate", ",", "so", "don’t", "be", "late", "We",
"surely", "shall", "sun", "shine", "soon", "would", "like",
"sit", "here", "cannot", "hear", "because", "of", "wood",
"band", "played", "its", "songs", "banned", "glamorous",
"night", "sketched", "a", "drone", "flying", "freaked", "out",
"when", "saw", "squirrel", "swimming", "man", "had", "cat",
" that", "was", "eating", "bug", "After", "dog", "got", "wet",
"Ed", "buy", "new", "pet", "My", "mom", "always", "tells",
"me", "beautiful", "eyes", "first", "went", "school", "wanted",
"die" };

while (input.hasNext()) {
String[] sample = input.next().split("\t");

for (int i = 0; i < valid_letters.length; i++) {
for (int j = 0; j < 1; j++) {
if (sample[j] == valid_letters[i]) {
boolean wrong = false;
System.out.print("break");
break;
}
}
}
if (wrong = true) {
num_wrong++;
}
}

// print out the results from the search
System.out
.print(" The number of wrong words in the first 13 sentences are "
+ num_wrong);
// Close the file
input.close();
}
}

例如文本文件包含:

I want to go to school little monkey 

程序应该返回的错误数是2

最佳答案

代码:

import java.util.Scanner;

public class main_class {

public static int num_wrong = 0;
public static java.io.File file = new java.io.File("text.txt");
public static String[] valid_letters = new String[130];
public static boolean wrong = true;
public static String[] sample = new String[190];

public static void main (String [] args) {
try {
text_file();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void text_file() throws Exception {
// Create Scanner to read file
Scanner input = new Scanner(file);

String [] valid_letters = { "I", " have ", " got ", "a", "date", "at",
"quarter", "to", "eight", "8", "7:45", "I’ll", "see", "you",
"the", "gate", ",", "so", "don’t", "be", "late", "We",
"surely", "shall", "sun", "shine", "soon", "would", "like",
"sit", "here", "cannot", "hear", "because", "of", "wood",
"band", "played", "its", "songs", "banned", "glamorous",
"night", "sketched", "a", "drone", "flying", "freaked", "out",
"when", "saw", "squirrel", "swimming", "man", "had", "cat",
" that", "was", "eating", "bug", "After", "dog", "got", "wet",
"Ed", "buy", "new", "pet", "My", "mom", "always", "tells",
"me", "beautiful", "eyes", "first", "went", "school", "wanted",
"die" };

while (input.hasNext()) {
// NOTE: split using space, i.e. " "
String[] sample = input.next().split(" ");

// NOTE: j < sample.length
for (int j = 0; j < sample.length; j++)
{
for (int i = 0; i < valid_letters.length; i++)
{
// NOTE: string comparison is using equals
if (sample[j].equals(valid_letters[i])) {

// NOTE: You want to update the variable wrong.
// And not create a local variable 'wrong' here!
wrong = false;
System.out.printf("%-12s is inside!%n",
"'" + valid_letters[i] + "'");
break;
}
}
if (wrong) {
num_wrong++;
}
// Reset wrong
wrong = true;
}
}

// Print out the results from the search
System.out.println("The number of wrong words in the first 13 sentences are "
+ num_wrong);
// Close the file
input.close();
}
}

输入(存储在“text.txt”中):

I want to go to school little monkey 

输出:

'I'          is inside!
'to' is inside!
'to' is inside!
'school' is inside!
The number of wrong words in the first 13 sentences are 4
//'go', 'want', 'little' and 'monkey' are not inside the String array

注意:

  • Value String 的比较使用 equals , 不是 == (用于 Reference 比较)
  • boolean wrong = false;创建一个局部变量
  • 你的 for 循环应该使用 j < sample.length
  • 应使用 " " 拆分字符串(空格),而不是 "\t" (制表符)

关于java - 找出文件中与字符串数组中的任何单词都不匹配的单词数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30818488/

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