Answers for "split string adding words and punctuation java"

0

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);
        }
    }
Posted by: Guest on March-09-2021

Code answers related to "split string adding words and punctuation java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language