Commit 55e80af8 authored by Adam Blank's avatar Adam Blank
Browse files

after lecture02

parent d60e9278
No related merge requests found
Showing with 3826 additions and 0 deletions
+3826 -0
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
/*
arrays
lists
maps (or dictionaries)
sets
*/
public class CountUniqueWords {
private static final String BOOK_FILENAME = "data/alice.txt";
public static void main(String[] args) throws FileNotFoundException {
//ArrayList<String> ds = new ArrayList<>();
HashSet<String> ds = new HashSet<>();
//HashMap!
Scanner s = new Scanner(new File(BOOK_FILENAME));
while (s.hasNext()) {
String next = s.next();
//if (!ds.contains(next)) {
ds.add(next);
//}
}
System.out.println(ds.size());
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class MostCommonWord {
private static final String BOOK_FILENAME = "data/alice.txt";
public static void main(String[] args) throws FileNotFoundException {
HashMap<String, Integer> ds = new HashMap<>();
Scanner s = new Scanner(new File(BOOK_FILENAME));
while (s.hasNext()) {
String word = s.next();
/*
if (ds.containsKey(word)) {
int whateverwasthere = ds.get(word);
ds.put(word, whateverwasthere + 1);
}
else {
ds.put(word, 1);
}
*/
/*
if (!ds.containsKey(word)) {
ds.put(word, 0);
}
ds.put(word, ds.get(word) + 1);
*/
ds.put(word, 1 + ds.getOrDefault(word, 0));
// if word in ds -> add 1
// otherwise, set word to 1
}
// loop through the keys and keep track of the max
int max = 0;
String word = "";
for (String w : ds.keySet()) {
if (ds.get(w) > max) {
max = ds.get(w);
word = w;
}
}
// print the word corresponding to the max
System.out.println(word);
}
}
\ No newline at end of file
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class MostCommonWordByChapter {
private static String BOOK_FILENAME = "data/alice.txt";
public static void main(String[] args) throws FileNotFoundException {
// chapter title
// word freq
HashMap<String, HashMap<String, Integer>> chapters = new HashMap<>();
//chapter 1: "the cow goes moo"
chapters.put("CHAPTER 1", new HashMap<>());
HashMap<String, Integer> inner = chapters.get("CHAPTER 1");
inner.put("moo", 1);
// XXX: Next line is not necessary
chapters.put("CHAPTER 1", inner);
}
}
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment