I want to rewrite the code below so it can t aking input from a file called Stri
ID: 3604857 • Letter: I
Question
I want to rewrite the code below so it can taking input from a file called String.txt import java.util.*; public class CharacterOccurrences { public static void countOccurrences(Map<Character, Integer> charCounts) { Scanner in = new Scanner(System.in); System.out.print("Enter a string: "); String str = in.nextLine(); char ch; for(int i = 0; i < str.length(); ++i) { ch = str.charAt(i); if(ch >= 'A' && ch <= 'Z') { ch += 32; } if(ch >= 'a' && ch <= 'z') { if(!charCounts.containsKey(ch)) { charCounts.put(ch, 0); } charCounts.put(ch, charCounts.get(ch) + 1); } } } public static void printResults(Map<Character, Integer> charCounts) { List<Map.Entry<Character, Integer>> list = new ArrayList<>(); for(Map.Entry<Character, Integer> entry: charCounts.entrySet()) { list.add(entry); } Map.Entry<Character, Integer> temp; for(int i = 0; i < list.size(); ++i) { for(int j = 0; j < list.size() - 1; ++j) { if(list.get(j).getKey() > list.get(j + 1).getKey()) { temp = list.get(j); list.set(j, list.get(j + 1)); list.set(j + 1, temp); } } } System.out.println("Map contains: "); System.out.println("Sort by keys"); System.out.println("Key Value"); for(int i = 0; i < list.size(); ++i) { System.out.println(list.get(i).getKey() + " " + list.get(i).getValue()); } for(int i = 0; i < list.size(); ++i) { for(int j = 0; j < list.size() - 1; ++j) { if(list.get(j).getValue() < list.get(j + 1).getValue()) { temp = list.get(j); list.set(j, list.get(j + 1)); list.set(j + 1, temp); } } } System.out.println("Sort by values "); System.out.println("Key Value"); for(int i = 0; i < list.size(); ++i) { System.out.println(list.get(i).getKey() + " " + list.get(i).getValue()); } } public static void main(String[] args) { Map<Character, Integer> map = new HashMap<>(); countOccurrences(map); printResults(map); } }
Explanation / Answer
Please find my modification.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class CharacterOccurrences {
public static void countOccurrences(Map<Character, Integer> charCounts) throws FileNotFoundException {
System.out.print("Reading a line from file String.txt: ");
Scanner in = new Scanner(new File("String.txt"));
String str = in.nextLine();
in.close();
char ch;
for(int i = 0; i < str.length(); ++i) {
ch = str.charAt(i);
if(ch >= 'A' && ch <= 'Z') {
ch += 32;
}
if(ch >= 'a' && ch <= 'z') {
if(!charCounts.containsKey(ch)) {
charCounts.put(ch, 0);
}
charCounts.put(ch, charCounts.get(ch) + 1);
}
}
}
public static void printResults(Map<Character, Integer> charCounts) {
List<Map.Entry<Character, Integer>> list = new ArrayList<>();
for(Map.Entry<Character, Integer> entry: charCounts.entrySet()) {
list.add(entry);
}
Map.Entry<Character, Integer> temp;
for(int i = 0; i < list.size(); ++i) {
for(int j = 0; j < list.size() - 1; ++j) {
if(list.get(j).getKey() > list.get(j + 1).getKey()) {
temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
System.out.println("Map contains: ");
System.out.println("Sort by keys");
System.out.println("Key Value");
for(int i = 0; i < list.size(); ++i) {
System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
}
for(int i = 0; i < list.size(); ++i) {
for(int j = 0; j < list.size() - 1; ++j) {
if(list.get(j).getValue() < list.get(j + 1).getValue()) {
temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
System.out.println("Sort by values ");
System.out.println("Key Value");
for(int i = 0; i < list.size(); ++i) {
System.out.println(list.get(i).getKey() + " " + list.get(i).getValue());
}
}
public static void main(String[] args) throws FileNotFoundException {
Map<Character, Integer> map = new HashMap<>();
countOccurrences(map);
printResults(map);
}
}
/*
Sample run:
Reading a line from file String.txt: Map contains:
Sort by keys
Key Value
e 2
f 2
i 4
j 1
l 2
m 2
n 1
o 1
r 1
s 2
t 1
y 1
Sort by values
Key Value
i 4
e 2
f 2
l 2
m 2
s 2
j 1
n 1
o 1
r 1
t 1
y 1
*/
###### String.txt ######
tjis is line from my file
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.