fork(1) download
  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.Random;
  7.  
  8. class LoginGenerator
  9. {
  10. private final List<String> words;
  11. private final int size;
  12. private Random rng = new Random();
  13.  
  14. public LoginGenerator(String dictionary) throws IOException
  15. {
  16. try (BufferedReader in = new BufferedReader(new FileReader(dictionary)))
  17. {
  18. words = new ArrayList<String>();
  19. String line;
  20. while ((line = in.readLine()) != null)
  21. {
  22. if (!line.endsWith("'s"))
  23. {
  24. words.add(line);
  25. }
  26. }
  27. size = words.size();
  28. }
  29. }
  30.  
  31. private String randomWord()
  32. {
  33. int randomIndex = rng.nextInt(size);
  34. return words.get(randomIndex);
  35. }
  36.  
  37. public String randomLogin()
  38. {
  39. return randomWord() + "-" + randomWord();
  40. }
  41.  
  42. public static void main(String[] args)
  43. {
  44. String dictionary = "/usr/share/dict/words";
  45. try
  46. {
  47. LoginGenerator generator = new LoginGenerator(dictionary);
  48. for (int i = 0; i < 100; ++i)
  49. {
  50. System.out.println(generator.randomLogin());
  51. }
  52. }
  53. catch (IOException ex)
  54. {
  55. System.out.println("error reading from " + dictionary);
  56. ex.printStackTrace();
  57. }
  58. }
  59. }
  60.  
Success #stdin #stdout #stderr 0.1s 320256KB
stdin
Standard input is empty
stdout
error reading from /usr/share/dict/words
stderr
java.io.FileNotFoundException: /usr/share/dict/words (No such file or directory)
	at java.io.FileInputStream.open(Native Method)
	at java.io.FileInputStream.<init>(FileInputStream.java:138)
	at java.io.FileInputStream.<init>(FileInputStream.java:93)
	at java.io.FileReader.<init>(FileReader.java:58)
	at LoginGenerator.<init>(Main.java:16)
	at LoginGenerator.main(Main.java:47)