split string adding words and punctuation java
public void readFile(String poem){
File file = new File(poem);
try(Scanner input = new Scanner(file)){
while(input.hasNextLine()){
String line = input.nextLine().toLowerCase();
//this splits at punctuation and saves it as a word
String[] lineToWord = line.split("\\b");
for(String word : lineToWord) {
if(!word.equals(" ")){
// this ignores spaces when adding to array
words.add(word);
}
}
}
}
catch (java.io.IOException ex) {
System.out.println("An error occurred while trying to read poem. Sadge: " + ex);
}
}