split string adding words and punctuation java

Solutions on MaxInterview for split string adding words and punctuation java by the best coders in the world

showing results for - "split string adding words and punctuation java"
Veronica
23 Jun 2018
1public void readFile(String poem){
2        File file = new File(poem);
3
4        try(Scanner input = new Scanner(file)){
5            while(input.hasNextLine()){
6                String line = input.nextLine().toLowerCase();
7                //this splits at punctuation and saves it as a word
8              	String[] lineToWord = line.split("\\b"); 
9                for(String word : lineToWord) {
10                    if(!word.equals(" ")){
11                      	// this ignores spaces when adding to array
12                        words.add(word);
13                    }
14                }
15            }
16        }
17        catch (java.io.IOException ex) {
18            System.out.println("An error occurred while trying to read poem. Sadge: " + ex);
19        }
20    }