格式化文本文件的输出 - Java

时间:2017-12-07 21:00:58

标签: java

如何更改代码,使其打印出来的方式与我希望打印出来的方式相同?我希望首先打印出userNameGenerator,在它前面放一个箭头( - >),然后在旁边打开和关闭方括号([])显示personName。我还想在最后删除逗号。我已经完成了一些,如下所示:

需要更改的代码:

Set<String> newStrSet = new HashSet<>();
for(int i = 0; i < personFile.size(); i++) {
    String[] regionSplit = personFile.get(i).getUserNameGenerator().split(", ");
    for(int j = 0; j < regionSplit.length; j++) {
    newStrSet.add(regionSplit[j]);
    }
}

for (String p: newStrSet) {
    System.out.printf("%s -> [", p);
    for (Codes2 s: personFile) {
        if (s.getUserNameGenerator().contains(p)) {
            System.out.printf("%s, ", s.getPersonName());
        }
    }  
    System.out.println("]");
}

完整代码:

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

public class Codes {
    public static void main(String[] args) { 
        List<Codes2> personFile = new ArrayList<>();

        try {   
            BufferedReader br = new BufferedReader(new FileReader("person-data.txt"));
            String fileRead = br.readLine();
            while (fileRead != null) {
                String[] personData = fileRead.split(":");                
                String personName = personData[0];
                String userNameGenerator = personData[1];                
                Codes2 personObj = new Codes2(personName, userNameGenerator);               
                personFile.add(personObj);
                fileRead = br.readLine();
            }
            br.close();
        }   

        catch (FileNotFoundException ex) {            
            System.out.println("File not found!");
        } 

        Set<String> newStrSet = new HashSet<>();
        for(int i = 0; i < personFile.size(); i++) {
            String[] regionSplit = personFile.get(i).getUserNameGenerator().split(", ");
            for(int j = 0; j < regionSplit.length; j++) {
                newStrSet.add(regionSplit[j]);
            }
        }

        for (String p: newStrSet) {
            System.out.printf("%s -> [", p);
            for (Codes2 s: personFile) {
            if (s.getUserNameGenerator().contains(p)) {
                System.out.printf("%s, ", s.getPersonName());
            }
        }  
        System.out.println("]");
    }       
}

Java类:

public class Codes2 implements Comparable<Codes2> {

    private String personName;
    private String userNameGenerator;

    public Codes2(String personName, String userNameGenerator) {
        this.personName = personName;
        this.userNameGenerator = userNameGenerator;
    }

    public String getPersonName() {
        return personName;
    }

    public String getUserNameGenerator() {
        return userNameGenerator;
    }

    @Override
    public int compareTo(Codes2 o) {
        return getUserNameGenerator().compareTo(o.getUserNameGenerator());
    }

    public int compare(Object lOCR1, Object lOCR2) {                
        return ((Codes2)lOCR1).userNameGenerator                        
                .compareTo(((Codes2)lOCR2).userNameGenerator);            
    }
}

输出:

Dompteuse -> [Imran Sullivan, ]
Deservedness -> [Eadie Jefferson, ]
Ostracize -> [Eadie Jefferson, ]
Abattoir -> [Angel Whitehouse, ]
Choreography -> [Imran Sullivan, Taylor Vargas, Priya Oliver, ]

我希望输出如何:

Dompteuse -> [Imran Sullivan]
Deservedness -> [Eadie Jefferson]
Ostracize -> [Eadie Jefferson]
Abattoir -> [Angel Whitehouse]
Choreography -> [Imran Sullivan, Taylor Vargas, Priya Oliver]

2 个答案:

答案 0 :(得分:2)

您需要更改处理打印的for循环,以便只有在personFile中有另一个元素时才打印逗号。

以下是我要尝试的内容(请注意,我无法测试此代码,因为我无法访问您的数据文件):

for (String p: newStrSet) {
    System.out.printf("%s -> [", p);
    boolean needComma = false;

    // Use indices instead of a for-each so you can 
    // check if there's a next element
    for (int i = 0; i < personFile.size(); ++i) {
        Codes2 s = personFile.get(i);

        if (s.getUserNameGenerator().contains(p)) {
            // Only print out the comma and space if there's a preceding element
            if (needComma) {
                System.out.print(", ");
            }

            System.out.printf("%s", s.getPersonName());

            needComma = true;
        }
    }

    System.out.println("]");
}

答案 1 :(得分:1)

我正在浏览每个循环的双重内容,我相信我已经缩小了你的问题。在每个循环的第二个循环中,打印出人物的名称,后跟逗号。但是你为每个元素做了这个,包括最后一个元素。在那里包含一个条件,一旦达到行的长度,只打印出名称而不是逗号。

for (Codes2 s: personFile) {
        if (s.getUserNameGenerator().contains(p)) {
           //Try including conditional for the last element here
            System.out.printf("%s, ", s.getPersonName());
        }
} 

希望这有帮助!