Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Words
- {
- /**
- Gets the middle character or character pair from this word
- when possible.
- @param word a word
- @return the middle character (if the word length is odd) or
- middle two characters (if it is even), or the empty string if
- the word is empty, or null if it is null.
- */
- public String getMiddle(String word)
- {
- if(word == null)
- {
- return null;
- }
- else if(word.length() == 0)
- {
- return " ";
- }
- else if(word.length() % 2 == 0)
- {
- return word.substring((int)(word.length()/2-1),(int)(word.length()/2+1));
- }
- if(word == null)
- {
- return "null";
- }
- else
- {
- return String.format("%c",word.charAt((int)(word.length()/2)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement