Guest User

Untitled

a guest
Jun 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.LinkedList;
  4. import java.util.Scanner;
  5.  
  6. public class Test{
  7.  
  8. public static void main(String [] args){
  9. LinkedList<String> list = turnSentencesToList("sampleFile.txt");
  10.  
  11. for(String s: list)
  12. System.out.println(s);
  13. }
  14.  
  15.  
  16. public static LinkedList<String> turnSentencesToList(String fileName) {
  17. LinkedList<String> list = new LinkedList<>();
  18.  
  19. File file = new File(fileName);
  20. Scanner scan;
  21. try {
  22. scan = new Scanner(file);
  23. while(scan.hasNextLine()){
  24. String line = scan.nextLine().trim();
  25.  
  26. String[] sentences = null;
  27. //we don't need empty lines
  28. if(!line.equals("")) {
  29. //splits by . or ! or ?
  30. sentences = line.split("\.|!|\?");
  31.  
  32. if(sentences!=null) {
  33. for(String s: sentences) {
  34. list.add(s.trim());
  35. }
  36. }
  37. }
  38. }
  39. } catch (FileNotFoundException e) {
  40. e.printStackTrace();
  41. return null;
  42. }
  43. return list;
  44. }
  45. }
  46.  
  47. Sample textfile
  48. A man, l, a ballot, a catnip, a pooh, a rail, a calamus, a dairyman, a bater, a canal - Panama
  49. This is a sentence
  50. This one also
  51. Heres another one
  52. Yes another one
Add Comment
Please, Sign In to add comment