Using the Scanner delimiter in Java, how do I keep the String that I am using as the delimiter? -
my program reads story file sentence sentence, using punctuation delimiter. stores sentences in arraylist , shuffles arraylist , prints creating different story every time run program. problem using delimiter gets rid of punctuation new story, there way can still use delimiter keep string part of reading?
you can use scanner's default white-space delimiter scan through file's content, use pattern/matcher find position of punctuation delimiter within each scanner token.
here's example:
final list<string> sentences = new arraylist(); final scanner scanner = new scanner(new file("story.txt")); final pattern pattern = pattern.compile("[.!?]"); stringbuilder sb = new stringbuilder(); // default white space delimiter while (scanner.hasnext()) { string token = scanner.next().trim(); // pattern in current token matcher matcher = pattern.matcher(token); if (matcher.find()) { // end position of match int index = matcher.end(); // add sentence substring beginning of token end match position sb.append(token.substring(0, index)); // build , add sentence sentences.add(sb.tostring().trim()); // start new sentence sb = new stringbuilder(token.substring(index)); } else { // no punctuation match, add token sentence sb.append(token); } // add space sentence sb.append(" "); } collections.shuffle(sentences); (string sentence : sentences) { system.out.println(sentence); }
you can scan single character @ time if language of story doesn't use white-space (eg. chinese).
hope helps!
Comments
Post a Comment