Ok I have a list of words that I need to have checked. How doI get it to check t
ID: 3613548 • Letter: O
Question
Ok I have a list of words that I need to have checked. How doI get it to check the words in the list and if the string that isbeing read has a keyword in it increase a counter likecounter++;public static void main(String[] args) throwsFileNotFoundException { List<String> list =Arrays.asList("abstract","assert","boolean","break","byte", "case","catch","char","class","const","continue","default","do","double", "else","enum","extends","for","final","finally","float","goto","if", "implements","import","instanceof","int","interface","long","native", "new","package","private","protected","public","return","short","static", "strictfp*","super","switch","synchronized","this","throw","throws", "transient","try","void","volatile","while"); Set<String> set = newTreeSet<String>(list); //System.out.println(set); Iterator<String>iterator = set.iterator(); while(iterator.hasNext()){ if(iterator.hasNext()){ Stringfilename = args[0];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a filename:: "); String fileName = keyboard.nextLine();
Scanner wordFile = new Scanner(newFileReader(fileName)); } else { System.out.println("No Keywords were found in file! "); } System.out.println(iterator.next() + ""); } } } Ok I have a list of words that I need to have checked. How doI get it to check the words in the list and if the string that isbeing read has a keyword in it increase a counter likecounter++;
public static void main(String[] args) throwsFileNotFoundException { List<String> list =Arrays.asList("abstract","assert","boolean","break","byte", "case","catch","char","class","const","continue","default","do","double", "else","enum","extends","for","final","finally","float","goto","if", "implements","import","instanceof","int","interface","long","native", "new","package","private","protected","public","return","short","static", "strictfp*","super","switch","synchronized","this","throw","throws", "transient","try","void","volatile","while"); Set<String> set = newTreeSet<String>(list); //System.out.println(set); Iterator<String>iterator = set.iterator(); while(iterator.hasNext()){ if(iterator.hasNext()){ Stringfilename = args[0];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a filename:: "); String fileName = keyboard.nextLine();
Scanner wordFile = new Scanner(newFileReader(fileName)); } else { System.out.println("No Keywords were found in file! "); } System.out.println(iterator.next() + ""); } } } public static void main(String[] args) throwsFileNotFoundException { List<String> list =Arrays.asList("abstract","assert","boolean","break","byte", "case","catch","char","class","const","continue","default","do","double", "else","enum","extends","for","final","finally","float","goto","if", "implements","import","instanceof","int","interface","long","native", "new","package","private","protected","public","return","short","static", "strictfp*","super","switch","synchronized","this","throw","throws", "transient","try","void","volatile","while"); Set<String> set = newTreeSet<String>(list); //System.out.println(set); Iterator<String>iterator = set.iterator(); while(iterator.hasNext()){ if(iterator.hasNext()){ Stringfilename = args[0];
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a filename:: "); String fileName = keyboard.nextLine();
Scanner wordFile = new Scanner(newFileReader(fileName)); } else { System.out.println("No Keywords were found in file! "); } System.out.println(iterator.next() + ""); } } }
Explanation / Answer
You have ScannerwordFile = new Scanner(newFileReader(fileName)); For every word you read in from the file, youneed to check if it's contained in your keywords set. int counter = 0; while(wordFile.hasNext()) { if(set.contains(wordFile.next())) { counter++; } } // output if(counter>0) { System.out.println(counter+" keywords found infile."); } else { System.out.println("Nokeywords were found in file!"); }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.