Welcome to my Page

Check out my GitHub here: Ranchonyx

Send me an Email here: Felix Janetzki

Ping me on Discord here: Kraut#2929

Repos:

A sample of my coding style.

This is the code for an application that counts occurrences of words in a text file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.io.*;
import java.util.HashMap;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) throws IOException {
        try {

            if(args[0].equals("-h") || args[0].equals("--help") || args[0].equals("/?")) {
                System.out.println("Usage: java -jar occurrences.jar <path: String>");
                System.exit(0);
            }

            String path = args[0];
            BufferedReader br = new BufferedReader(new FileReader(path));
            String content = br.lines().collect(Collectors.joining(System.lineSeparator()));
            br.close();

            String[] words = content.replaceAll(",","").replaceAll("\\.","").replaceAll("\n","").split(" ");
            HashMap<String, Integer> occurrenceMap = new HashMap<String, Integer>();

            for(String w : words) {
                if(occurrenceMap.containsKey(w)) {
                    occurrenceMap.put(w, occurrenceMap.get(w)+1);
                } else {
                    occurrenceMap.put(w, 1);
                }
            }

            for (String key : occurrenceMap.keySet()) {
                System.out.println(key+" : "+occurrenceMap.get(key));
            }

        } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
            System.out.println("No path argument given, exiting.");
            System.exit(1);
        }

    }
}