For this question you will write a Java program that will ta
For this question, you will 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:
would produce output similar to the following:
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 once. 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 a2q1out.txt.
In general, assume that all output goes to the screen using System.out.println unless the assignment explicitly tells you to write it to a file — like this one does.
a2a.txt:
Solution
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class tester{
public static void main(String args[]){
Scanner in = null;
try {
in = new Scanner(new File(\"a2a.txt\"));
} catch (FileNotFoundException e) {
}
Map<String, Integer> countryWise = new HashMap<String, Integer>();
Map<String, Integer> eventWise = new HashMap<String, Integer>();
while(in.hasNext()){
String country = in.nextLine();
String eventString = in.nextLine();
String tempString = in.nextLine();
if(countryWise.containsKey(country)){
int count = countryWise.get(country);
countryWise.put(country, count + 1);
}
else{
countryWise.put(country, 1);
}
if(eventWise.containsKey(eventString)){
int count = eventWise.get(eventString);
eventWise.put(eventString, count + 1);
}
else{
eventWise.put(eventString, 1);
}
}
FileWriter fw = null;
try {
fw = new FileWriter(new File(\"a2q1out.txt.\"));
} catch (IOException e) {
}
System.out.println(\"Count of gold medallists by country:\");
try {
fw.write(\"Count of gold medallists by country:\ \");
} catch (IOException e) {
}
for(String key:countryWise.keySet()){
System.out.println(key + \" - \" + countryWise.get(key));
try {
fw.write(key + \" - \" + countryWise.get(key) + \"\ \");
} catch (IOException e) {
}
}
System.out.println(\"\ Count of gold medallists by event type:\");
try {
fw.write(\"\ Count of gold medallists by event type:\ \");
} catch (IOException e) {
}
for(String key:eventWise.keySet()){
System.out.println(key + \" - \" + eventWise.get(key));
try {
fw.write(key + \" - \" + eventWise.get(key) + \"\ \");
} catch (IOException e) {
}
}
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

