java - Using HashSet to store a text file and read from it -


i've seen lot of great resources regarding hassets, nothing helps me particular problem. i'm taking algorithms class on generics , assignment requires txt file read system using scanner (which done) , using hashset, load txt file can read user input , find number of occurrences of word. have method returning words , have of hashset , file reader code done. i'm stuck on how store whole txt file 1 hashset. couldn't work doing crime.add , tried several other things. missing easier way implement method? thanks

edit: assignment instructions - program 1 (70 points) load java.util.hashset the words novel “crime , punishment”, theodore dostoevsky (text file available on blackboard assignment). prompt user enter word , report whether or not word appears in novel.

edit: ok, have of written , runs not finding words in txt file, somewhere went wrong adding file hashset. ideas? i've tried array list, different string implementations , don't know turn. helpful info.

import java.awt.list; import java.io.file; import java.io.filenotfoundexception; import java.util.hashset; import java.util.scanner; import java.util.set;  public class candphashset {     public static void main(string[] args) throws filenotfoundexception{         scanner file = new scanner(new file("crime_and_punishment.txt")).usedelimiter("[ˆa-za-z]+");         scanner input = new scanner(system.in);          set<string> crime = new hashset<string>();          while(file.hasnext()){             string line = file.nextline();             //string[] words = line.split("[ˆa-za-z]+");             (string word : line.split("[ˆa-za-z]+")){                 crime.add(line);             }         }          string search;         system.out.println("enter word search for: ");         search = input.next();          if(crime.contains(input)){             system.out.println("yes");         }else{             system.out.println("no");         }     } } 

it looks don't need count word occurrences. need split input file string individual words, , store them hashset<string>. should use method contains() check if word given user present in set.

there couple of problems in code should check:

  • the way use usedelimiter() in scanner not correct. don't want specify delimiter whitespace, default, used.

  • if using whitespace scanner delimiter split input words. don't need read file line line.

  • you use crime.contains(input) user provided word. input scanner, not string. want use crime.contains(search).

the revised code this:

// read file using whitespace delimiter (default) // input split words scanner file = new scanner(new file("crime_and_punishment.txt"));  set<string> crime = new hashset<>(); // each word in input while (file.hasnext()) {     // convert word lower case, trim , insert set     // in step, want remove punctuation marks     crime.add(file.next().trim().tolowercase()); }  system.out.println("enter word search for: "); scanner input = new scanner(system.in); // convert input lowercase string search = input.next().tolowercase();  // check if set contains search string if (crime.contains(search)) {     system.out.println("yes"); } else {     system.out.println("no"); } 

Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -