-1

in the task I have to read a list of names stored in a column in a remote file. I have to read them from the resource identified by the URL (I have already done this) and then I am using a set to draw 15 names without repeat (random) names. I have no idea how to do it, I was looking at google, but unfortunately I did not find the answer to my problem. I am asking for help and guidance

import java.util.HashSet;
import java.util.Set;
import java.net.*;
import java.io.*;


public class Race {

    public static void main(String[] args) throws Exception {
            URL oracle = new URL("http://szgrabowski.kis.p.lodz.pl/zpo17/nazwiska.txt");
            BufferedReader in = new BufferedReader(
            new InputStreamReader(oracle.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
            System.out.println(inputLine);
            Set<String> nameList = new HashSet<String>();
            nameList.add(inputLine); 


            in.close();

    }
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Enechi
  • 27
  • 3
  • 2
    Welcome to SO! Could you post a [mcve] of what you have so far? – Michael Berry May 02 '18 at 14:24
  • Title of this question doesn't seem to be related to problem you are trying to solve. Please try to improve it and clarify your question, possibly by posting example of what you have and what you want to achieve. – Pshemo May 02 '18 at 14:28
  • 2
    For now I am guessing you are looking for something like: [Take n random elements from a List?](https://stackoverflow.com/q/4702036) (just fill that list with content of set) – Pshemo May 02 '18 at 14:29
  • Are you looking for 15 random names from the list of names present in the URL (http://szgrabowski.kis.p.lodz.pl/zpo17/nazwiska.txt) ? – Amit May 02 '18 at 14:39
  • yes I`m looking for 15 random names from the list in this URL – Enechi May 02 '18 at 14:48
  • You might want to have a look at Knuth, TAoCP, Algorithm S 3.4.2. That appears to be close to what you require. – rossum May 02 '18 at 18:58

1 Answers1

1

You should split your code logically into separate methods. As an example:

  1. List<String> readTextFromUrl(URL)
  2. Set<String> getNDistinctElements(List<String>, int)
  3. String pickRandomElement(Set<String>)

That way, you can test each of these methods separately.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588