From 4e9c44c6f92ea45e803b6dfaf13f5c889daae9f2 Mon Sep 17 00:00:00 2001 From: Kendrick Hang Date: Tue, 14 Feb 2023 20:55:06 -0800 Subject: [PATCH 1/3] Initial commit --- .idea/vcs.xml | 6 +++++ src/WordFrequency.java | 55 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 .idea/vcs.xml diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 2f82ee4..6a650c4 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -1,5 +1,60 @@ +<<<<<<< HEAD public class WordFrequency { public static void main(String[] args) { System.out.println("Hello world!"); +======= +import java.io.File; +import java.io.FileNotFoundException; +import java.util.Map; +import java.util.Scanner; +import java.util.TreeMap; + +public class WordFrequency { + public static void main(String[] args) throws FileNotFoundException { + System.out.println("Hello world!"); + + + Map files = new TreeMap<>(); + //in method main, implement code to read in a file and count + // up the number of times each word occurs in the file + + + Scanner infile = new Scanner(new File("medTale.txt")); + while (infile.hasNext()){ + String word = infile.next(); + //check to see if words is in the map + if (files.containsKey(word)){ + //get the existing value out + int value = files.get(word); + + //increment the count (value) + value++; + //update/put the value back in + files.put(word,value); + + }else { + //put word into the map + files.put(word,1); + + } + + }//end of While loop + System.out.println(files); + + + //write a loop to walk through items in the map + for (String key: files.keySet()){ + int value = files.get(key); + System.out.println("word " + key + " appears " + value + " times"); + } + + for (int value : files.values()){ + System.out.println(value); + } + //cant look at value to get the key. only at key to get the value + + + +>>>>>>> 3e47cf5 (Initial commit) } } \ No newline at end of file From 80d882a6de3a791ed831acb657b9bc714923c68d Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Tue, 21 Feb 2023 18:25:21 -0800 Subject: [PATCH 2/3] Initial commit --- src/WordFrequency.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index 6a650c4..e03dfb0 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -35,7 +35,6 @@ public static void main(String[] args) throws FileNotFoundException { }else { //put word into the map files.put(word,1); - } }//end of While loop From 862086d381fb53566a2cc5b6bda25943bb75f982 Mon Sep 17 00:00:00 2001 From: sukanvisapearyoo Date: Tue, 21 Feb 2023 18:27:40 -0800 Subject: [PATCH 3/3] fixed it and used try catch --- src/WordFrequency.java | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/WordFrequency.java b/src/WordFrequency.java index e03dfb0..ea764a4 100644 --- a/src/WordFrequency.java +++ b/src/WordFrequency.java @@ -1,8 +1,5 @@ -<<<<<<< HEAD -public class WordFrequency { - public static void main(String[] args) { - System.out.println("Hello world!"); -======= + + import java.io.File; import java.io.FileNotFoundException; import java.util.Map; @@ -35,6 +32,7 @@ public static void main(String[] args) throws FileNotFoundException { }else { //put word into the map files.put(word,1); + } }//end of While loop @@ -51,9 +49,5 @@ public static void main(String[] args) throws FileNotFoundException { System.out.println(value); } //cant look at value to get the key. only at key to get the value - - - ->>>>>>> 3e47cf5 (Initial commit) } } \ No newline at end of file