0

I have an input files as follows:

conf/iastedCSN/KangHPLNL06 Quoc V. Phung    
conf/iastedCSN/KangHPLNL06 Kungmeng Lo  
conf/iastedCSN/KangHPLNL06 Hoang Nam Nguyen 
conf/iastedCSN/KangHPLNL06 M. M. Lee    
series/sci/ZighedAB13 Djamel Abdelkader Zighed  
series/sci/ZighedAB13 Rafik Abdesselam  
series/sci/ZighedAB13 Ahmed Bounekkar   
series/sci/LermanG13 Isra챘l-C챕sar Lerman

And I want to the output with delimiter as follows:

conf/iastedCSN/KangHPLNL06 | QuocV.Phung    
conf/iastedCSN/KangHPLNL06 | KungmengLo 
conf/iastedCSN/KangHPLNL06 | HoangNamNguyen 
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | DjamelAbdelkaderZighed  
series/sci/ZighedAB13 | RafikAbdesselam 
series/sci/ZighedAB13 | AhmedBounekkar  
series/sci/LermanG13 | Isra챘l-C챕sarLerman

However for now, I got lots of lines duplicated in the result and I could not seem to find out why. The output I got as follows:

conf/iastedCSN/KangHPLNL06 | QuocV.Phung    
conf/iastedCSN/KangHPLNL06 | KungmengLo 
conf/iastedCSN/KangHPLNL06 | HoangNamNguyen 
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | DjamelAbdelkaderZighed  
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | RafikAbdesselam 
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/ZighedAB13 | AhmedBounekkar  
conf/iastedCSN/KangHPLNL06 | M.M.Lee    
series/sci/LermanG13 | Isra챘l-C챕sarLerman   
series/sci/ZighedAB13 | AhmedBounekkar  

Below is the source code:

package authorgraph;
import java.io.*;
import java.util.*;

public class graph {
    private static BufferedReader br;

    public static void main(String[] args)
    {
        try{
        br = new BufferedReader (new FileReader ("inproceedings-author-test1.txt"));
        Map<String, String>  items = new TreeMap<String, String>();
        String line;

        while (null != (line = br.readLine()))
        {
            String[] line_parts = line.split(" ");
            if (line_parts.length > 1)
            {
                StringBuilder name = new StringBuilder(line_parts[1]);
                for (int i = 2; i < line_parts.length; i++)
                {
                    name.append(line_parts[i]);
                }
                items.put(new String(line_parts[0]), name.toString());
            }
            for (String conf: items.keySet())
            {
                System.out.println(conf + " | " + items.get(conf));
            }
        }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
};

Any help would be really appreciated

1 Answer 1

2

The new String in items.put(new String(line_parts[0]), name.toString()); is useless. String being immutable, if you already have a String, there is no need to create a new one.

And you got duplicate because the for is not placed at the right place: you are executing it each time you read a line.

        for (String conf: items.keySet())
        {
            System.out.println(conf + " | " + items.get(conf));
        }
      } // end of while

Do that instead: move the for outside the while loop.

      } // end of while
        for (String conf: items.keySet())
        {
            System.out.println(conf + " | " + items.get(conf));
        }

And instead of doing a loop on keySet(), do that:

        for (Map.Entry<String, String> entry : items.entrySet()) {
            System.out.println(entry.getKey() + " | " + entry.getValue());
        }

Map<K,V>.entrySet() will return a Map.Entry<K,V>. An entry being an association between a key and its value.

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

2 Comments

Could you edit your question with an extract of the expected output ?
I edited it with the expected output. And I just got the result by removing the map.entry and just write the normal string instead.

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.