Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.LinkedList;
- import java.util.Scanner;
- public class Test{
- public static void main(String [] args){
- LinkedList<String> list = turnSentencesToList("sampleFile.txt");
- for(String s: list)
- System.out.println(s);
- }
- public static LinkedList<String> turnSentencesToList(String fileName) {
- LinkedList<String> list = new LinkedList<>();
- File file = new File(fileName);
- Scanner scan;
- try {
- scan = new Scanner(file);
- while(scan.hasNextLine()){
- String line = scan.nextLine().trim();
- String[] sentences = null;
- //we don't need empty lines
- if(!line.equals("")) {
- //splits by . or ! or ?
- sentences = line.split("\.|!|\?");
- if(sentences!=null) {
- for(String s: sentences) {
- list.add(s.trim());
- }
- }
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- return null;
- }
- return list;
- }
- }
- Sample textfile
- A man, l, a ballot, a catnip, a pooh, a rail, a calamus, a dairyman, a bater, a canal - Panama
- This is a sentence
- This one also
- Heres another one
- Yes another one
Add Comment
Please, Sign In to add comment