-1

How can I change the code so that it prints out like the way I want it to be printed out? I want the userNameGenerator to be printed out first, with an arrow (->) in front of it, and the personName to be displayed with open and close square brackets ([]) beside it. I also want to remove the commas at the end. I've done a bit of it already as you can see below:

Code that needs changing:

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("]");
}

Full code:

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 Class:

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);            
    }
}

Output:

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

How I want the output to look like:

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

2 Answers 2

1

You need to change the for loop that handles the printing so that it only prints the comma if there is another element in the personFile.

Here's what I would try (note that I can't test this code since I don't have access to your data file):

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("]");
}
Sign up to request clarification or add additional context in comments.

5 Comments

The comma and closed bracket prints out in a new line
There was a problem with the brace placement. It should be working now.
Something wrong with the code because this is what happen in the console: Choreography -> [, , , , , , , , Taylor Vargas, , , , , , , ]
I hadn't realized that you weren't printing certain people. Does this work better?
Some of them work, some of them done work: [Tyler Green, Andre Bishop] [Angelina Rose, ]
1

I am looking through the double for each loop and I believe I have narrowed down your issue. In the second for each loop, you print out the name of the person followed by the comma. But you do this for every element, including the last one. Include a conditional in there that once it reaches the length of the row, only print out the name not the comma.

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

Hope this helps!

Comments

Your Answer

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