0

This data is saved in data.txt I am trying to write a program that can arrange

18b0885  // this is the registration number, bullet points to show indentation 
   SS844 Parallel Algorithms        //  These are course taken by student
   SS555 Calculus for Distributed Computing
   SS501 Quantum Communication

17b0585
   SS828 Problem Based Programming
   SS844 Parallel Algorithms 
   SS567 Hacking Quantum Network

17b2582
   SS567 Hacking Quantum Network
   SS844 Parallel Algorithms 
   SS501 Quantum Communication

A big list of data like this, And need to write a program to short this data is ascending order by registration number and course will follow the registration number. so the expected out put is like this.

Out put will, course Title and the students registration number who are taking that course sample:

SS501 Quantum Communication
     18b0885
     17b2582

SS567 Hacking Quantum Network
     17b2582
     17b0585

SS844 Parallel Algorithms 
     17b2582
     17b0585
     18b0885

and so on, Which method will be easier to do this

It doesn't give the desired answer

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Set;
import java.util.TreeSet;

public class Sort3 {
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new FileReader("data2018.txt"));
    //   Map<String, List<String>> map = new TreeMap<String, List<String>>();
    Map<String, Set<String>> map = new TreeMap<String, Set<String>>();
    String line = reader.readLine();//read header
    while ((line = reader.readLine()) != null) {
      String key = getField(line); // 
      //  List<String> l = map.get(key);
      Set<String> l = map.get(key);
      if (l == null) {
        // l = new LinkedList<String>();
        l = new TreeSet<String>();
        map.put(key, l);
      }
      l.add(line);

    }
    reader.close();
    FileWriter writer = new FileWriter("sorted_numbers4.txt");
    writer.write("");
//     for (List<String> list : map.values()) {
//      for (String val : list) {
//       writer.write(val);
//       writer.write("\n");
//      }
//     }
    for (Set<string> key : map.keySet()){
//print key (subject) here
      writer.write("Subject="+key+"\n");
      for (Set<String> list : map.get(key)) {
        for (String val : list) {
          writer.write(val);
          writer.write("\n");
        }
      }
    }

    writer.close();
  }

  private static String getField(String line) {
    return line.split(",")[0];// 
  }
}

The above program out puts into another text file like this

    SS501 Quantum Communication
    SS555 Calculus for Distributed Computing
    SS567 Hacking Quantum Network
    SS567 Hacking Quantum Network
    SS660 Genetic Computation
    SS828 Problem Based Programming
    SS844 Parallel Algorithms
    SS876 Positronics
    SS880 Quark-based Logic

    17b2582
    17b0585
    18b0885

Any recommendation to modify to get desired answer?

5
  • 2
    By asking whether it can be done, how are we supposed to know? Did you compile and run the program? Does it produce the expected output? Commented Nov 5, 2013 at 4:08
  • Does it work when you try? If not, what errors or problems are you getting? Commented Nov 5, 2013 at 4:10
  • It looks to me like your code is not populating the map the way you expect. The key appears to be the same as the field (probably both the course and registration numbers). You need to adjust it to remember the registration number and add it to the values saved at each map location (whose keys are the course numbers). Commented Nov 5, 2013 at 4:28
  • pmorken thanks for suggestion can you help to solve thanks. Commented Nov 5, 2013 at 4:34
  • possible duplicate of Sorting in Java from external text file Commented Nov 5, 2013 at 5:15

1 Answer 1

1

Instead of Map<String, List<String>> map = new TreeMap<String, List<String>>(); use Map<String, Set<String>> map = new TreeMap<String, Set<String>>(); and then use a TreeSet instead of a LinkedList e.g. l = new TreeSet<String>(); This ensures that list (set) entries are unique and sorted.

Now while writing out, iterate first on KeySet of the Map and then iterate the values (set) associated with each keys, something like below pseudo-code:

for (String key : map.keySet()){
//print key (subject) here
writer.write("Subject="+key+"\n");
for (Set<String> list : map.get(key)) {
      for (String val : list) {
       writer.write(val);
       writer.write("\n");
      }
}
}

EDIT: parsing logic- from your sample data, I can see that course names begin with "S" character while registration numbers are hex/numeric. may be you could use this information while parsing the data.

Sign up to request clarification or add additional context in comments.

6 Comments

Can you Clarify @Bimalesh Jha
what you do not understand?
I did the changes you have suggested here, but it doesn't run gives error, for (Set<String> list : map.get(key)) incompatible types required: java.util.Set<java.lang.String> found: java.lang.String can u please help to fix this
In your original code you were putting List<string> in the map. I suggested to change to put Set<string> in the map. But, looks like you are puuting raw String. hence, it is giving the error. Pls update your question with new code so that we can see what you are doing now.
Please check the code I have updated, Still gives the same error incompatible type
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.