Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program prints out the right output but it needs to be in alphabetical orde

ID: 3824135 • Letter: T

Question

This program prints out the right output but it needs to be in alphabetical order.

this is the .txt file used in the command line for the code:

Here is my program:

import java.io.*;
import java.util.*;
public class BloodBank{
public static void main(String[] args) throws Exception{
BufferedReader infile = new BufferedReader( new FileReader( "type2pres.txt" ) );
String thisLine = null;
// Declaring Map to store president names with their blood groups
Map<String, String> map = new HashMap<String, String>();
Map<String, String> reverse_map = new TreeMap<String, String>();
while ((thisLine = infile.readLine()) != null) {
// Logic for processing the file and preparing the Map to store the President names along with their respective blood groups
String[] splited = thisLine.split(" ");
for (String part : splited) {
String[] splitLine = part.split(",");
String indexVal = splitLine[0];
String president_names = "";
if(indexVal.equals("O")){
Arrays.sort(splitLine);
String o_group_president_names = "";
for(int i = 0; i < splitLine.length; i++){
if(!splitLine[i].equals("O")){
o_group_president_names = o_group_president_names + " " + splitLine[i];
reverse_map.put(splitLine[i],indexVal);
}
}
map.put(indexVal, o_group_president_names);
}else{
Arrays.sort(splitLine);
for(int i = 1; i < splitLine.length; i++){
president_names = president_names + " " + splitLine[i];
reverse_map.put(splitLine[i],indexVal);
}
map.put(indexVal, president_names);
}
}
System.out.println();
}
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(" "+entry.getKey() + " : " + entry.getValue());
}
System.out.println();
for (Map.Entry<String, String> entry : reverse_map.entrySet()) {
System.out.println(" "+entry.getKey() + " : " + entry.getValue());
}
} // MAIN
} // BLOODBANK

above is what the output is supposed to look like.

below is what my output looks like

B+ : F.D.Roosevelt J.A.Garfield T.L.Hoffman
A+ : B.H.Obama G.W.Bush J.F.Kennedy
B- :
A- : A.Lincoln G.A.Ford L.B.Johnson U.S.Grant W.J.Clinton
O : C.A.Arthur G.Cleveland R.M.Nixon

A.Lincoln : A-
B.H.Obama : A+
C.A.Arthur : O
F.D.Roosevelt : B+
G.A.Ford : A-
G.Cleveland : O
G.W.Bush : A+
J.A.Garfield : B+
J.F.Kennedy : A+
L.B.Johnson : A-
R.M.Nixon : O
T.L.Hoffman : B+
U.S.Grant : A-
W.J.Clinton : A-

Command Prompt CNUs erst im Desktop an java an A+ B. H. Obama G. W. Bush J. F. Kennedy A. Lincoln G.A. Ford L. B. Johnson U. S. Grant W. J. Clint on B+ F. D. Roosevelt J. A. Garfield T. L. Hoffman C. A. Arthur G.Cleveland R.M.Nixon A. Lincoln B. H. alma A+ C. A. Arthur F. D. Roosevelt B+ G. A. Ford G. Cleveland G. W. Bush A+ J. A. Garfield B+ J. F. Kennedy A+ L. B. Johnson R. M. Nixon T. L. Hoffman B+ U.S. Grant W. J. Clinton C: Users tim Desktop an

Explanation / Answer

import java.io.*;
import java.util.*;
public class BloodBank{
public static void main(String[] args) throws Exception{
BufferedReader infile = new BufferedReader( new FileReader( "type2pres.txt" ) );
String thisLine = null;
// Declaring Map to store president names with their blood groups
Map<String, String> map = new TreeMap<String, String>();
Map<String, String> reverse_map = new TreeMap<String, String>();
while ((thisLine = infile.readLine()) != null) {
// Logic for processing the file and preparing the Map to store the President names along with their respective blood groups
String[] splited = thisLine.split(" ");
for (String part : splited) {
String[] splitLine = part.split(",");
String indexVal = splitLine[0];
String president_names = "";
if(indexVal.equals("O")){
Arrays.sort(splitLine);
String o_group_president_names = "";
for(int i = 0; i < splitLine.length; i++){
if(!splitLine[i].equals("O")){
o_group_president_names = o_group_president_names + " " + splitLine[i];
reverse_map.put(splitLine[i],indexVal);
}
}
map.put(indexVal, o_group_president_names);
}else{
Arrays.sort(splitLine);
for(int i = 1; i < splitLine.length; i++){
president_names = president_names + " " + splitLine[i];
reverse_map.put(splitLine[i],indexVal);
}
map.put(indexVal, president_names);
}
}
System.out.println();
}
for (Map.Entry<String, String> entry : map.entrySet()) {
System.out.println(" "+entry.getKey() + " : " + entry.getValue());
}
System.out.println();
for (Map.Entry<String, String> entry : reverse_map.entrySet()) {
System.out.println(" "+entry.getKey() + " : " + entry.getValue());
}
} // MAIN
} // BLOODBANK

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote