Advertisement
advictoriam

Untitled

Dec 11th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.83 KB | None | 0 0
  1. public class Words
  2. {
  3.    /**
  4.       Gets the middle character or character pair from this word
  5.       when possible.
  6.       @param word a word
  7.       @return the middle character (if the word length is odd) or
  8.       middle two characters (if it is even), or the empty string if
  9.       the word is empty, or null if it is null.
  10.    */
  11.    public String getMiddle(String word)
  12.    {
  13.       if(word == null)
  14.       {
  15.          return null;
  16.       }
  17.       else if(word.length() == 0)
  18.       {
  19.          return " ";
  20.       }
  21.       else if(word.length() % 2 == 0)
  22.       {
  23.          return word.substring((int)(word.length()/2-1),(int)(word.length()/2+1));
  24.       }
  25.       if(word == null)
  26.       {
  27.          return "null";
  28.       }
  29.       else
  30.       {
  31.          return String.format("%c",word.charAt((int)(word.length()/2)));
  32.       }
  33.    }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement