Java 找出文件中与字符串数组中的任何单词都不匹配的单词数
我正在尝试创建一个从文件中读取数据的程序。我希望每次都检查文件中的下一个单词是否与特定字符串数组中的特定单词匹配 每次单词不匹配时,我希望将时间记录为(错误的++),并打印文件中的单词与字符串数组中至少一个单词不匹配的次数 这是我的节目:Java 找出文件中与字符串数组中的任何单词都不匹配的单词数,java,arrays,regex,string,Java,Arrays,Regex,String,我正在尝试创建一个从文件中读取数据的程序。我希望每次都检查文件中的下一个单词是否与特定字符串数组中的特定单词匹配 每次单词不匹配时,我希望将时间记录为(错误的++),并打印文件中的单词与字符串数组中至少一个单词不匹配的次数 这是我的节目: public class main_class { public static int num_wrong; public static java.io.File file = new java.io.File("text.txt");
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();
}
}
程序应该返回的错误数是
2
我可以看到两个错误
if(wrong = true)
我想你是说
if(wrong == true)
for (int j= 0 ; j < sample[i].length ; j ++ )
此外,此循环:
for (int j= 0 ; j < 1 ; j ++ )
for(int j=0;j<1;j++)
将只在j=0时执行,因为它将在之后立即停止。我想你是说
if(wrong == true)
for (int j= 0 ; j < sample[i].length ; j ++ )
for(int j=0;j
我可以看到两个错误
if(wrong = true)
我想你是说
if(wrong == true)
for (int j= 0 ; j < sample[i].length ; j ++ )
此外,此循环:
for (int j= 0 ; j < 1 ; j ++ )
for(int j=0;j<1;j++)
将只在j=0时执行,因为它将在之后立即停止。我想你是说
if(wrong == true)
for (int j= 0 ; j < sample[i].length ; j ++ )
for(int j=0;j
使用类似于列表的集合,这样就不用每次都迭代字符串数组,只需执行List.contains(“word”)即可
List validWords=new ArrayList
有效词语。添加(“I”);
validWords.add(“更多单词”);
int错误计数=0;
while(input.hasNext())
{
String[]sample=input.next().split(“\t”);
对于(int i=0;i
注意:在方法级别重新声明全局变量是错误的。有效字母、样本。您可以在main方法中声明它们时初始化它们,并且不必指定静态数组大小
第二,一旦找到第一个匹配的单词,您的代码就会从循环中中断,这样您就可以避免检查剩余的单词,这是您想要做的吗?如果是这样,您可以相应地编辑我的代码 使用类似于列表的集合,这样就不用每次迭代字符串数组,只需执行List.contains(“word”)即可
List validWords=new ArrayList
有效词语。添加(“I”);
validWords.add(“更多单词”);
int错误计数=0;
while(input.hasNext())
{
String[]sample=input.next().split(“\t”);
对于(int i=0;i
注意:在方法级别重新声明全局变量是错误的。有效字母、样本。您可以在main方法中声明它们时初始化它们,并且不必指定静态数组大小
第二,一旦找到第一个匹配的单词,您的代码就会从循环中中断,这样您就可以避免检查剩余的单词,这是您想要做的吗?如果是这样,您可以相应地编辑我的代码 代码:
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();
}
}
'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
输出:
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();
}
}
'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
注意:
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();
}
}
'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
字符串比较使用的是值
,而不是等于
(用于=
比较)参考
布尔错误=错误代码>创建局部变量
- for循环应该使用
j
- 字符串应使用
(空格)拆分,而不是使用“
(制表符)拆分”\t“
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();
}
}
'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
输出:
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();
}
}
'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
注意:
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();
}
}
'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
字符串比较使用的是值
,而不是等于
(用于=
比较)参考
布尔错误=错误代码>创建局部变量
- for循环应该使用
j
- 字符串应使用
(空格)拆分,而不是使用“
(制表符)拆分”\t“
如果你希望单词列表发生变化 如果单词列表不变,您可以避免将单词拆分,并将三元树生成一个完整的正则表达式trie。然后执行“全部查找”以获取列表中没有的所有单词 这个正则表达式trie是一种非常快速的方法 您可以使用此试用应用程序从单词列表自动生成正则表达式 regexformat.com.
将其设置为不区分大小写和空白字边界 只需将输出组调整为负前瞻,如下所示
# "(?i)(?<!\\S)(?!(?:,|7:45|8|a(?:fter|lways|t)?|b(?:an(?:d|ned)|e(?:autiful|cause)?|u(?:g|y))|ca(?:nnot|t)|d(?:ate|ie|o(?:g|n’t)|rone)|e(?:ating|d|ight|yes)|f(?:irst|lying|reaked)|g(?:ate|lamorous|ot)|h(?:a(?:d|ve)|e(?:ar|re))|i(?:ts|’ll)?|l(?:ate|ike)|m(?:an|e|om|y)|n(?:ew|ight)|o(?:f|ut)|p(?:et|layed)|quarter|s(?:aw|chool|ee|h(?:all|ine)|it|ketched|o(?:ngs|on)?|quirrel|u(?:n|rely)|wimming)|t(?:ells|h(?:at|e)|o)|w(?:a(?:nted|s)|e(?:nt|t)?|hen|o(?:od|uld))|you)(?!\\S))\\S+(?!\\S)"
(?i)
(?<! \S )
(?!
(?:
,
| 7:45
| 8
| a
(?: fter | lways | t )?
| b
(?:
an
(?: d | ned )
| e
(?: autiful | cause )?
| u
(?: g | y )
)
| ca
(?: nnot | t )
| d
(?:
ate
| ie
| o
(?: g | n’t )
| rone
)
| e
(?: ating | d | ight | yes )
| f
(?: irst | lying | reaked )
| g
(?: ate | lamorous | ot )
| h
(?:
a
(?: d | ve )
| e
(?: ar | re )
)
| i
(?: ts | ’ll )?
| l
(?: ate | ike )
| m
(?: an | e | om | y )
| n
(?: ew | ight )
| o
(?: f | ut )
| p
(?: et | layed )
| quarter
| s
(?:
aw
| chool
| ee
| h
(?: all | ine )
| it
| ketched
| o
(?: ngs | on )?
| quirrel
| u
(?: n | rely )
| wimming
)
| t
(?:
ells
| h
(?: at | e )
| o
)
| w
(?:
a
(?: nted | s )
| e
(?: nt | t )?
| hen
| o
(?: od | uld )
)
| you
)
(?! \S )
)
\S+
(?! \S )
#“(?i)(?如果您想快速执行此操作,可以动态创建三元树或哈希
如果你希望单词列表发生变化
如果单词列表不变,您可以避免将单词拆分,并将三元树生成一个完整的正则表达式trie。然后执行“全部查找”以获取列表中未包含的所有单词
这个正则表达式trie是一种非常快速的方法
您可以使用此试用应用程序从单词列表自动生成正则表达式
regexformat.com.
将其设置为不区分大小写和空白字边界
只需将输出组调整为负前瞻,如下所示
# "(?i)(?<!\\S)(?!(?:,|7:45|8|a(?:fter|lways|t)?|b(?:an(?:d|ned)|e(?:autiful|cause)?|u(?:g|y))|ca(?:nnot|t)|d(?:ate|ie|o(?:g|n’t)|rone)|e(?:ating|d|ight|yes)|f(?:irst|lying|reaked)|g(?:ate|lamorous|ot)|h(?:a(?:d|ve)|e(?:ar|re))|i(?:ts|’ll)?|l(?:ate|ike)|m(?:an|e|om|y)|n(?:ew|ight)|o(?:f|ut)|p(?:et|layed)|quarter|s(?:aw|chool|ee|h(?:all|ine)|it|ketched|o(?:ngs|on)?|quirrel|u(?:n|rely)|wimming)|t(?:ells|h(?:at|e)|o)|w(?:a(?:nted|s)|e(?:nt|t)?|hen|o(?:od|uld))|you)(?!\\S))\\S+(?!\\S)"
(?i)
(?<! \S )
(?!
(?:
,
| 7:45
| 8
| a
(?: fter | lways | t )?
| b
(?:
an
(?: d | ned )
| e
(?: autiful | cause )?
| u
(?: g | y )
)
| ca
(?: nnot | t )
| d
(?:
ate
| ie
| o
(?: g | n’t )
| rone
)
| e
(?: ating | d | ight | yes )
| f
(?: irst | lying | reaked )
| g
(?: ate | lamorous | ot )
| h
(?:
a
(?: d | ve )
| e
(?: ar | re )
)
| i
(?: ts | ’ll )?
| l
(?: ate | ike )
| m
(?: an | e | om | y )
| n
(?: ew | ight )
| o
(?: f | ut )
| p
(?: et | layed )
| quarter
| s
(?:
aw
| chool
| ee
| h
(?: all | ine )
| it
| ketched
| o
(?: ngs | on )?
| quirrel
| u
(?: n | rely )
| wimming
)
| t
(?:
ells
| h
(?: at | e )
| o
)
| w
(?:
a
(?: nted | s )
| e
(?: nt | t )?
| hen
| o
(?: od | uld )
)
| you
)
(?! \S )
)
\S+
(?! \S )
#“(?i)(?+l
但是,您忘记了主要更改..\t
不起作用..它应该是(空格)
或\\s
@karthikmanchala对了,换了不少地方,忘了!谢谢!!你真的帮了我,我的朋友谢谢!恭喜!你们两个都忘了主要的改动。\t
不起作用。(空格)
或\\s
@karthikmanchala对,换了不少地方,忘了这个!谢谢!!你真的帮了我,我的朋友谢谢你!恭喜!你们两个事实上我都知道,但我只是想展示我认为他想说的话,以及他说的话