Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PositionWordCombiner {
- private String original,words[];
- int charsCount;
- public PositionWordCombiner(String word, String delimeter){
- setWords(word,delimeter);
- setCharCount();
- }
- public String toString(){
- return getWordsCombined();
- }
- public String getWordsCombined(){
- StringBuilder result = new StringBuilder("ORIGINAL MSG : ");
- result.append(getOriginalWord());
- result.append("\n");
- result.append("COMBINED WORDS : ");
- for(int ch = 0; ch < getCountOFChars(); ++ch)
- result.append(getCharsAt(ch));
- return result.toString();
- }
- private String getCharsAt(int pos){
- StringBuilder result = new StringBuilder();
- for(String s : words)
- result.append(s.charAt(pos));
- return result.toString();
- }
- private void setWords(String w,String d){
- original = w;
- words = w.split(d);
- }
- /*
- * to avoid an IndexOutBoundException
- * we need to know the length of the
- * word considered to be smallest
- */
- private void setCharCount(){
- charsCount = getSmallestWord().length();
- }
- public String getSmallestWord(){
- String smallest = words[0];
- for(int i = 1; i < getNumOfWords(); ++i)
- if(words[i].compareTo(smallest) < 0)
- smallest = words[i];
- return smallest;
- }
- public String getOriginalWord(){
- return original;
- }
- public int getCountOFChars(){
- return charsCount;
- }
- public int getNumOfWords(){
- return words.length;
- }
- }
- public class WordsCombinerDemo {
- public static void main(String[] args){
- PositionWordCombiner[] pwc = new PositionWordCombiner[3];
- for(int i = 0; i < pwc.length; ++i){
- String input = JOptionPane.showInputDialog("enter word" + (i + 1));
- pwc[i] = new PositionWordCombiner(input," ");
- }
- //display results
- for(int i = 0; i < pwc.length; ++i){
- System.out.printf("WORD # %d \n%s \n\n",i + 1,pwc[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement