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

Unable to get the help I want. I am trying a picture to explain Input import jav

ID: 3847560 • Letter: U

Question

Unable to get the help I want. I am trying a picture to explain

Input

import java.io.*;
import java.util.*;

public class jobHuntOrganizer {

public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("temp.txt"));
Map<String, String> map=new TreeMap<String, String>();
String line="";
while((line=reader.readLine())!=null){
System.out.println(line);
map.put(getField(line),line);
}
reader.close();
FileWriter writer = new FileWriter("output.txt");
for(String val : map.values()){
writer.write(val);
writer.write(' ');
}
writer.close();
}

private static String getField(String lines) {
return lines;
}
}

Wrong Output:

I want this output:

1 bloomb LRC 74363 Web ux 29441 software technology internship 5 blood cup helpdesk manager IT Network Administrator & Systems Support 9 techno inc helpdesk/desktop support L 0 2 Linium Recruiting helpdesk analyst

Explanation / Answer

Here is the code and output. Please don't forget to rate the answer if it helped. Thank you very much.

import java.io.*;

import java.util.*;

public class jobHuntOrganizer {

   public static void main(String[] args) throws Exception {

       BufferedReader reader = new BufferedReader(new FileReader("temp.txt"));

      

       //add a comparator to the map. We want to compare the string ignoring the case.

       //the treemap object will use this comparator to do comparison between 2 keys

       Map<String, String> map = new TreeMap<String, String>(new Comparator<String>() {

           @Override

           public int compare(String o1, String o2) {

               return o1.compareToIgnoreCase(o2);

           }

          

       });

      

       String key = "", value="",line;

       //read a key. Then keep reading all lines and collect them as value until a blank line is seen

       while ((key = reader.readLine()) != null) {

           value = "";

           while((line = reader.readLine()) !=null)

           {

               if(line.trim().equals("")) //is it a blank line. then finished reading all lines for value

                   break;

               value += line + " ";

           }

             

           map.put(key, value);

       }

       reader.close();

       FileWriter writer = new FileWriter("output.txt");

       for (String k : map.keySet() ){

           writer.write(k+ " "+ map.get(k));

           writer.write(' ');

       }

       writer.close();

   }

   private static String getField(String lines) {

       return lines;

   }

}

output.txt

blood cup

   helpdesk manager

   IT Network Adminsitrator & Systems Support

bloomb LRC:

   74363 web ux

   29441 software technology internship

Linium Recruiting

   helpdesk analyst

techno inc

   helpdesk/desktop support