Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- try {
- Task5();
- } catch (IOException e) {
- System.out.println(e.getMessage());
- }
- }
- public static void Task5() throws IOException {
- BufferedReader in = null;
- BufferedReader in1 = null;
- BufferedWriter out = null;
- String[] words = null;
- HashMap<String, Integer> results = new HashMap<>();
- try {
- in = new BufferedReader(new FileReader("words.txt"));
- in1 = new BufferedReader(new FileReader("text.txt"));
- out = new BufferedWriter(new FileWriter("results.txt"));
- StringBuilder word = new StringBuilder();
- String currLine = null;
- while ((currLine = in.readLine()) != null) {
- word.append(currLine);
- }
- words = word.toString().split(",");
- currLine = null;
- while ((currLine = in1.readLine()) != null) {
- for (String s : words) {
- Pattern p = Pattern.compile(s);
- Matcher m = p.matcher(currLine);
- if (currLine.contains(s)) {
- int br = 0;
- while (m.find()) {
- br++;
- }
- if (results.containsKey(s)) {
- Integer count = results.get(s);
- results.put(s, count + br);
- } else {
- results.put(s, br);
- }
- }
- }
- }
- for (Entry e : results.entrySet()) {
- out.write(e.getKey() + " " + e.getValue() + " times" + System.getProperty("line.separator"));
- }
- } catch (FileNotFoundException e) {
- System.out.println(e.getMessage());
- } catch (IOException e) {
- throw new IOException("I/O stream error!");
- } finally {
- if (in != null) {
- in.close();
- }
- if (in1 != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- }
- System.out.println("Done");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement