Fancy looping in Java
<p>I have a problem wherein I have two strings, the length of one of which I will know only upon execution of my function. I want to write my function such that it would take these two stings and based upon which one is longer, compute a final string as under - </p>

<pre><code>finalString = longerStringChars1AND2
   + shorterStringChar1 
   + longerStringChars3and4 
   + shorterStringChar2 
   + longerStringChars5AND6
</code></pre>

<p>...and so on till the time the SHORTER STRING ENDS. </p>

<p>Once the shorter string ends, I want to append the remaining characters of the longer string to the final string, and exit. I have written some code, but there is too much looping for my liking. Any suggestions?</p>

<p>Here is the code I wrote - very basic - 
        public static byte [] generateStringToConvert(String a, String b){
            (String b's length is always known to be 14.)</p>

<pre><code>    StringBuffer stringToConvert = new StringBuffer(); 

    int longer = (a.length()&gt;14) ? a.length() : 14;
    int shorter = (longer &gt; 14) ? 14 : a.length();

    int iteratorForLonger = 0;
    int iteratorForShorter = 0;
            while(iteratorForLonger &lt; longer) {
                int count = 2;
                while(count&gt;0){
                    stringToConvert.append(b.charAt(iteratorForLonger));

                    iteratorForLonger++;
                    count--; 
                    }

             if(iteratorForShorter &lt; shorter &amp;&amp; iteratorForLonger &gt;= longer){
                 iteratorForLonger = 0;
             }
             if(iteratorForShorter&lt;shorter){
                 stringToConvert.append(a.charAt(iteratorForShorter));
                 iteratorForShorter++;
             }
             else{
                 break;
             }


            }
    if(stringToConvert.length()&lt;32 | iteratorForLonger&lt;b.length()){
        String remainingString = b.substring(iteratorForLonger);
        stringToConvert.append(remainingString);
    }
    System.out.println(stringToConvert);
    return stringToConvert.toString().getBytes();

}
</code></pre>
