Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TextClass {
- private String text;
- /*
- Getter and Setter
- */
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- /*
- Constructors
- */
- public TextClass(String text) {
- this.text = text;
- }
- public TextClass() {
- this.text = "default";
- }
- /*
- Methods
- */
- public int numberOfWords(String text) {
- String trim = text.trim();
- if (trim.isEmpty())
- return 0;
- return trim.split("\\s+").length; // \s regex metacharacter which finds whitespace character and split returns array of splitted words
- }
- public int numberOfCharacters(String text) {
- String trim = text.trim();
- return trim.toCharArray().length;
- }
- public int numberOfSentences(String text) {
- String trim = text.trim();
- if(trim.equals("")){
- return 0;
- }
- int i;
- int counter = 0; //for counting sentences. Only the dot (.) seperates the sentences in this case
- for (i = 0; i < trim.length(); i++) {
- if (trim.charAt(i) == '.')
- counter++;
- }
- if(counter == 0){
- return counter += 1;
- }
- return counter;
- }
- public boolean doesWordExist(String word, String text) {
- if (text.indexOf(word) != -1) {
- return true;
- } else {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment