lundi 4 septembre 2017

Random String from a collection of strings in a file

import java.io.*;
import java.lang.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;

public class LunchTableGenerator {
    static LinkedHashSet<String> lunch = new LinkedHashSet<String>();

    public static void main(String[] args) throws Exception {
        final File inputFile = null;
        final PrintWriter outputFile = null;
        final File reusedFile = null;
        read(inputFile, lunch);
        writeFile(outputFile, lunch);
} 
    //file is read
    public static void read(File inputFile, LinkedHashSet<String> lunch) throws Exception{
        Iterator lunchIterator = lunch.iterator(); //hasNext returns false??
        BufferedReader reader = new BufferedReader(new FileReader("all_emails.txt"));
        String line = reader.readLine();
        while (line != null) {
            String[] emails = line.split(";"); //currently in an array
            for (String email : emails) {
                String cleanedUpWord = email;
                lunch.add(cleanedUpWord);
        }
                line = reader.readLine();
    }
    }
    //file is written with random func, part 1: randoms are written in, but dupes are still found
    //1. eliminate dupes
    //2. make sure that 1 invited list file doesnt repeat guests
    public static void writeFile(PrintWriter outputFile, LinkedHashSet<String> lunch) throws Exception{
        Random rand = new Random();
        List<String> lunch2 = new ArrayList<String>(lunch); //make a copy of the original set into a new list 
        List<String> list = new ArrayList<String>(lunch); //make the lunch set into a list
        List<String> dupes = new ArrayList<String>();
        List<String> example = new ArrayList<String>();

        Set<String> uniqueSet = new LinkedHashSet<String>();
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        try {
            outputFile = new PrintWriter("prev_attendees.txt");
            for(int y=0; y<input; y++) {
            Collections.shuffle(list);
                 Set<String> uniqueWriteSet = new LinkedHashSet<String>(list);
             List<String> uniqueWriteList = new ArrayList<String>(uniqueWriteSet);
             //remove dupes by adding the list to the set, then add the set back to the list
//           uniqueWriteSet.addAll(uniqueWriteList);
//           uniqueWriteList.clear();
//           uniqueWriteList.addAll(uniqueWriteSet);
             list = list.stream().distinct().collect(Collectors.toList());

                outputFile.println(list.get(input));
            //  System.out.println(uniqueWriteList);
                sc.close();

                BufferedReader temp = new BufferedReader(new FileReader("prev_attendees.txt"));
                String line = temp.readLine();
                while (line != null) {
                    String[] tempArray = line.split(";"); //currently in an array
                    for (String email : tempArray) {
                        String cleanedUpWord = email;
                        dupes.add(cleanedUpWord); //take these elements and remove them from the original pool
                }
                        line = temp.readLine();
                        //lunch.removeAll(dupes);


                }

            }

        }finally{
               outputFile.flush();
               outputFile.close();
            }
}
}

I am trying to generate a random number of strings from a collection of Strings inside of a file and I am trying to avoid duplicates everytime I call outputFile.println. I have tried a lot of things but I cant seem to eliminate some of the duplicates that I encounter. The input list is read from a file that doesnt contain any duplicates. I am simply reading the file and picking a string at random from the file and printing a certain number of strings(the number is based on the user input i.e scanner object) into an output file. Could someone point me in the right direction on how I would eliminate duplicates from the output file, so I only have a distinct number of strings in the output file.

all_emails.txt contains

LAST1 first1 <first1.last1@gmail.com>;
LAST2 first2 <first2.last2@gmail.com>;
LAST3 first3 <first3.last3@gmail.com>;

prev_attendees.txt contains the generated output

Thank you for the help!




Aucun commentaire:

Enregistrer un commentaire