For this question, you wll write a Java program that will tally and print the co
ID: 3596269 • Letter: F
Question
For this question, you wll write a Java program that will tally and print the counts of gold medals for an international sporting event according to two categorizations: by country, and by event type. The input comes from a text file where the information is not stored grouped by either country or event type: you will have to assemble the results in your program The text file you will be reading contains three lines of text for each medal win. The first line is the three-letter country code, the second is the event type, and the third is the specific event. For example CHN Diving Women's 10m Platform CAN Rowing Men's Eight CHN Rowing Women's Quadruple Scu1ls would produce output similar to the following: Count of gold meda1lists by country: CHN 2 CAN 1 Count of gold medall1sts by event type: Diving 1 Rowing 2 End of processing. The order of the countries and the events in the output does not matter, but each country and each event type should only be listed ance. Assume there are no errors in the input file Make no assumptions about the number of countries, events, or winners, except that there are at most 500 of each The input should come from a file named a2a.txt. Write the output both to the console using System.out.println and to a text file named a2qlout.txt. in general, assume that all output goes to the screen using System.out.printl1n unless the assignment explicitly tells you to write it to a file- like this one does top PART B: READABILITY [10 MARKS] This program will read in the contents of a text file containing a normal text document and reorganize its contents by separately storing each sentence of the text For our purposes the end of a sentence is marked by any word that ends with one of the three characters 2Explanation / Answer
//Answer of the 1st question //
package chegg;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
public class Olympics {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String,Integer> countryMap = new HashMap<String,Integer>();
HashMap<String,Integer> eventMap = new HashMap<String,Integer>();
try {
File file = new File("test.txt"); // give the path as per your convenience
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
int counter = 0;
while ((line = bufferedReader.readLine()) != null) {
if(counter % 3 == 0){
if(countryMap.containsKey(line)) // increasing the count in case of getting a matching country
{
countryMap.put(line, countryMap.get(line)+1);
}
else
countryMap.put(line,1); // initial count is 1;
}
if(counter%3==1)
{
if(eventMap.containsKey(line)) // increasing the count in case of getting a matching event
{
eventMap.put(line, eventMap.get(line)+1);
}
else
eventMap.put(line,1); // initial count is 1;
}
counter++;
}
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Count of gold medel by country");
for(String str : countryMap.keySet())
{
System.out.println(str + " " + countryMap.get(str));
}
System.out.println("Count of gold medel by event");
for(String str : eventMap.keySet())
{
System.out.println(str + " " + eventMap.get(str));
}
}
}
// Sample output //
Count of gold medel by country
can 1
chn 2
Count of gold medel by event
divig 1
rowing 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.