Skip to content

Commit e9e5e69

Browse files
committed
studio counting character. Reading from files
1 parent 7d4d5ae commit e9e5e69

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.io.IOException;
2+
import java.nio.charset.*;
3+
import java.nio.file.Files;
4+
import java.util.*;
5+
import java.nio.file.Paths;
6+
import java.nio.file.Path;
7+
8+
public class charactersInString {
9+
public static void main (String[] arg){
10+
String quote = "If the product of two terms is zero then common sense says at least one of the two terms has to be zero to start with. So if you move all the terms over to one side, you can put the quadratics into a form that can be factored allowing that side of the equation to equal zero. Once you’ve done that, it’s pretty straightforward from there.";
11+
12+
//items for reading from user input
13+
// Scanner input = new Scanner(System.in);
14+
// System.out.println("Input the line you want to know the character count for: ");
15+
// String line = input.nextLine();
16+
17+
try {
18+
//reading from file
19+
Path filepath = Paths.get("/Users/sonnienguyen/Desktop/Java Projects/java-web-dev-projects/control-flow-and-collections/studio/counting-characters/src/main/java/test.txt");
20+
String content = Files.readString(filepath, StandardCharsets.UTF_8);
21+
HashMap<Character, Integer> countCharMap = new HashMap<Character, Integer>();
22+
//counting characters
23+
char[] charactersInString = content.replaceAll("[^a-zA-Z0-9]","").toUpperCase().toCharArray();
24+
for(char c : charactersInString) {
25+
if(countCharMap.containsKey(c)) {
26+
countCharMap.put(c, countCharMap.get(c) + 1);
27+
} else {
28+
countCharMap.put(c, 1);
29+
}
30+
}
31+
//outputting characters
32+
for(Map.Entry<Character, Integer> entry : countCharMap.entrySet()) {
33+
System.out.println(entry.getKey() + ": " + entry.getValue());
34+
}
35+
} catch (IOException e) {
36+
System.out.println("An error occured: " + e.getMessage());
37+
}
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
La, la la, la la la la la
2+
La, la la, la la la la la
3+
I don't need no one, two, three, not bourgeoisie, I'll step in
4+
Know just how to make a scene, just me myself my best friends
5+
Uh-huh, I like it, it's simple as that
6+
That's right, uh-huh
7+
I like it, I keep coming back
8+
I don't need nobody tryna tell me how to run it
9+
Found myself some baddies 'bout to turn it to a hundred
10+
Uh-huh, they like it, it's simple as that
11+
That's right, uh-huh, they like it, they keep coming back
12+
Oh, we-ee-ee ain't flexin' babe we do what we do
13+
Wanna pull up on us? Check in at the gate and come thru
14+
We can run it run it run it no this ain't a debut
15+
Love me once I know you'll love me twice
16+
Love me once the naughty turns to nice
17+
La, la la, la la la la la
18+
I don't need no cookie cutter perfect ordinary
19+
Can find me under ride or die inside that dictionary
20+
Babe, uh-huh, they like it, it's simple as that
21+
That's right, uh-huh, they like it, they keep coming back
22+
Oh, we-ee-ee ain't flexin' babe we do what we do
23+
Wanna pull up on us? Check in at the gate and come thru
24+
We can run it run it run it no this ain't a debut
25+
Love me once I know you'll love me twice
26+
Love me once the naughty turns to nice
27+
Oh, we-ee-ee ain't flexin' babe, we do what we do
28+
Wanna pull up on us? Check in at the gate and come thru
29+
We can run it run it run it no this ain't a debut
30+
Love me once I know you'll love me twice
31+
Love me once the naughty turns to nice
32+
La, la la, la la la la la
33+
La, la la, la, love me once I know you'll love me twice
34+
La, la la, la la la la la
35+
Love me once I know you'll love me twice
36+
Love me once it's time to roll the dice

0 commit comments

Comments
 (0)