java - Find out number of words in file that do not match any word in the String array -
i trying create program reads data file. want each time check if next word file matches specific word specific string array.
each time words don't match, want keep track of times (wrong++) , print number of times words file didn't match @ least 1 word string array.
here program:
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 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"); (int = 0; < valid_letters.length; i++) { (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 results search system.out .print(" number of wrong words in first 13 sentences " + num_wrong); // close file input.close(); } }
the text file example contains:
i want go school little monkey
and program should return number of mistakes 2
.
code:
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 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 (int j = 0; j < sample.length; j++) { (int = 0; < valid_letters.length; i++) { // note: string comparison using equals if (sample[j].equals(valid_letters[i])) { // note: want update variable wrong. // , not create local variable 'wrong' here! wrong = false; system.out.printf("%-12s inside!%n", "'" + valid_letters[i] + "'"); break; } } if (wrong) { num_wrong++; } // reset wrong wrong = true; } } // print out results search system.out.println("the number of wrong words in first 13 sentences " + num_wrong); // close file input.close(); } }
input (stored in "text.txt"):
i want go school little monkey
output:
'i' inside! 'to' inside! 'to' inside! 'school' inside! number of wrong words in first 13 sentences 4 //'go', 'want', 'little' , 'monkey' not inside string array
note:
value
comparison of string usingequals
, not==
( usedreference
comparision)boolean wrong = false;
creates local variable- your loop should using
j < sample.length
- the string should split using
" "
(space), , not"\t"
(tab)
Comments
Post a Comment