summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2014-12-03 14:45:17 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2014-12-03 14:45:17 +0100
commit8a53a8fca255e4a84ca0e35dac925a223738b98a (patch)
tree1a1123823752be5334787cd0d5bf17c8aff1a43d /src/de/fhswf
parent286b3fc8adab72327330b0d4562176e23353342f (diff)
downloadJava1-8a53a8fca255e4a84ca0e35dac925a223738b98a.tar.gz
Java1-8a53a8fca255e4a84ca0e35dac925a223738b98a.zip
Assignment No.9 after correction
Diffstat (limited to 'src/de/fhswf')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java61
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java7
2 files changed, 61 insertions, 7 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java b/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java
index fcd5373..a6d90ef 100644
--- a/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java
+++ b/src/de/fhswf/in/inf/java1/aufgabe9/WordCount.java
@@ -7,7 +7,9 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
+import java.util.Collections;
import java.util.Map.Entry;
+import java.util.Set;
import java.util.TreeMap;
/**
@@ -22,15 +24,26 @@ public class WordCount
private TreeMap<String, Integer> wordMap = new TreeMap<>();
/**
- * Reads the file and counts the words.
+ * Empty constructor.
+ *
+ */
+ public WordCount()
+ {
+
+ }
+
+ /**
+ * Reads a file and counts the words.
*
* @param file
- * The file to be counted.
+ * The file to be read.
*/
- public WordCount(File file)
+ public final void readFile(File file)
{
try (BufferedReader f = new BufferedReader(new FileReader(file)))
{
+ wordMap.clear();
+
String line = null;
while ((line = f.readLine()) != null)
@@ -44,9 +57,10 @@ public class WordCount
{
word = word.toLowerCase();
- if (wordMap.containsKey(word))
+ Integer tmp = wordMap.get(word);
+ if (tmp != null)
{
- wordMap.put(word, wordMap.get(word) + 1);
+ wordMap.put(word, tmp + 1);
}
else
{
@@ -79,4 +93,41 @@ public class WordCount
return ret.toString();
}
+
+ /**
+ * Returns all words from the file.
+ *
+ * @return An unmodifiable Set of the words from the file.
+ */
+ public final Set<String> getWords()
+ {
+ return Collections.unmodifiableSet(wordMap.keySet());
+ }
+
+ /**
+ * Returns the count of a word in a file.
+ *
+ * @param word
+ * The file to be counted.
+ * @return Returns the count of findings in the file.
+ */
+ public final int getCount(String word)
+ {
+ if (word == null)
+ {
+ throw new IllegalArgumentException("Word must be a valid referece.");
+ }
+ if (word.isEmpty())
+ {
+ throw new IllegalArgumentException("The word can't be empty.");
+ }
+
+ Integer tmp = wordMap.get(word);
+ if (tmp == null)
+ {
+ return 0;
+ }
+ return tmp;
+ }
+
}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java b/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java
index e024529..a1364c9 100644
--- a/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java
+++ b/src/de/fhswf/in/inf/java1/aufgabe9/WordCountTest.java
@@ -31,9 +31,12 @@ public final class WordCountTest
public static void main(String[] args)
{
File file = new File(
- "/home/aentfs/workspace/Java1/misc/Blatt9TestFile2.txt");
+ "C:/eclipse/workspace/java1/misc/Blatt9TestFile2.txt");
- System.out.println(new WordCount(file));
+ WordCount tmp = new WordCount();
+ tmp.readFile(file);
+ System.out.println(tmp);
+ System.out.println("fgh = " + tmp.getCount("fgh"));
}