Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.Character;
- /**
- * Write a description of class TextLine here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class TextLine
- {
- // instance variables - replace the example below with your own
- private String text;
- /**
- * Constructor for objects of class TextLine
- */
- public TextLine()
- {
- // initialise instance variables
- text = "Test my methods.";
- }
- public TextLine(String s)
- {
- text = s;
- }
- public TextLine(TextLine t)
- {
- t = new TextLine(text);
- }
- public int countChars()
- {
- return text.length();
- }
- public int countWords()
- {
- String[] words = new String[0];
- if(text.contains(" "))
- {
- words = text.split(" ");
- int wordCount = words.length;
- return wordCount;
- }
- else if(text.isEmpty())
- {
- return 0;
- }
- return 1;
- }
- public int countLetters()
- {
- char[] characterArray = new char[0];
- for(int i = 0; i < text.length(); i++)
- {
- char character = text.charAt(i);
- if(Character.isLetter(i))
- {
- characterArray = text.toCharArray();
- return characterArray.length;
- }
- else if(text.isEmpty())
- {
- return 0;
- }
- }
- return characterArray.length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement