Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GetBigWords {
- // write a method getBigWords that will take one string and return an array of strings of
- // that are of 5 letters or more.
- public static void main(String[] args) {
- // provide a string
- String name= "once upon a time there was a great castle";
- // call the method getBigWords
- String[] a =getBigWords(name);
- // print is only to check the method here
- for (String e: a)
- System.out.print(e+" ");
- }
- public static String[] getBigWords(String a){
- create a large array of strings
- String [] words= new String[100];
- // create a variable to store individual words
- String word = "";
- // the counter is to keep up with the count of individual words over 5 letters long
- int count=0;
- // a loop to check each character if it is a letter
- for (int i=0; i<a.length(); i++){
- char temp = a.charAt(i);
- // use the Character class isLetter() method to check if true
- if (Character.isLetter(temp)){
- // if true add that character to word
- word+=a.charAt(i);
- // if this is the last character of the string, add the word to the array if it is long enough
- if (i==a.length()-1 && word.length()>4)
- words[count] = word;
- }
- // if character is not a letter and less than 5 reset word variable to empty
- else if (word.length()<5)
- word = "";
- // if the character is not a letter and the word is long enough do these things
- else{
- words[count]= word;
- word="";
- count++;
- }
- }
- // for aesthetic reasons only. To return an array that is only as large as the number of words
- String[] array = new String[count+1];
- System.arraycopy(words,0,array,0,array.length);
- return array;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement