Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a better way to do this method...?  RSS feed

 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem:
Had to write a boolean method isEndOfSentence() that has a single value char parameter c. The method returns true if c is either a period, a question mark, or an exclamation point, otherwise the method returns false..

My Solution:



Question one:
Does my method solve the problem?

Question two:
Seems to me there should be an easier way to do this? Is there...

Thanks,

Pete
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static boolean isEndOfSentence( char c ){
return String.valueOf(c).matches("[.?!]");
}
 
Pete Tyo
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static boolean isEndOfSentence( char c ){
return String.valueOf(c).matches("[.?!]");
}
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

scroll down the page a bit for "Character Classes"
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how about:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!