Advertisement
Guest User

Strings&Testing

a guest
Jan 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.51 KB | None | 0 0
  1. import java.lang.Character;
  2. /**
  3.  * Write a description of class TextLine here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. public class TextLine
  9. {
  10.     // instance variables - replace the example below with your own
  11.     private String text;
  12.  
  13.     /**
  14.      * Constructor for objects of class TextLine
  15.      */
  16.     public TextLine()
  17.     {
  18.         // initialise instance variables
  19.         text = "Test my methods.";
  20.     }
  21.     public TextLine(String s)
  22.     {
  23.         text = s;
  24.     }
  25.     public TextLine(TextLine t)
  26.     {
  27.         t = new TextLine(text);
  28.     }
  29.    
  30.     public int countChars()
  31.     {
  32.         return text.length();
  33.     }
  34.     public int countWords()
  35.     {
  36.         String[] words = new String[0];
  37.         if(text.contains(" "))
  38.         {
  39.             words = text.split(" ");
  40.             int wordCount = words.length;
  41.             return wordCount;
  42.         }
  43.         else if(text.isEmpty())
  44.         {
  45.             return 0;
  46.         }
  47.         return 1;
  48.     }
  49.     public int countLetters()
  50.     {
  51.         char[] characterArray = new char[0];
  52.         for(int i = 0; i < text.length(); i++)
  53.         {
  54.             char character = text.charAt(i);
  55.             if(Character.isLetter(i))
  56.             {
  57.                 characterArray = text.toCharArray();
  58.                 return characterArray.length;
  59.             }
  60.             else if(text.isEmpty())
  61.             {
  62.                 return 0;
  63.             }
  64.         }
  65.         return characterArray.length;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement