Advertisement
advictoriam

Untitled

Jan 20th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. public class Words
  2. {
  3.    /**
  4.       Returns the nth short word (length <= 3) in an array.
  5.       @param words an array of strings
  6.       @param n an integer > 0
  7.       @return the nth short word in words, or the empty string if there is
  8.       no such word
  9.    */
  10.    public String nthShortWord(String[] words, int n)
  11.    {
  12.       int count = 1;
  13.       for(String a : words)
  14.       {
  15.          if(a.length() <= 3)
  16.          {
  17.             if(count == n){return a;}
  18.             else{count++;}
  19.          }
  20.       }
  21.       return "";
  22.    }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement