LoganBlackisle

opg4

Sep 3rd, 2019
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. package opgave4;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.NoSuchElementException;
  6. import java.util.Scanner;
  7. import java.util.Set;
  8. import java.util.TreeSet;
  9.  
  10. public class fromFiletoTreeSet {
  11.  
  12. public static void main(String[] args) throws NoSuchElementException {
  13. Set<String> words = new TreeSet<>();
  14. int counter = 0;
  15. try {
  16. File fileIn = new File("C:\\Users\\Allan Birch\\Desktop\\Skole\\PRO\\Lektioner\\2. Semester\\Lektion 5 - Collection Framework\\Gjøngehøvdingen.txt");
  17. Scanner scan = new Scanner(fileIn);
  18. while (scan.hasNext()) {
  19. String s = scan.next();
  20. words.add(s);
  21. counter++;
  22. }
  23. System.out.println("There are " + counter + " words in the Set: " + words);
  24. scan.close();
  25. } catch (IOException e) {
  26. System.out.println(e.getMessage());
  27. }
  28. }
  29. }
Add Comment
Please, Sign In to add comment