title: Morgan and a String
body: <p>I am trying to solve this question: <a href="https://www.hackerrank.com/challenges/morgan-and-a-string" rel="nofollow">https://www.hackerrank.com/challenges/morgan-and-a-string</a></p>
  
  <p>Here's my code:</p>
  
  <pre><code>import java.io.*;
  import java.util.*;
  import java.text.*;
  import java.math.*;
  import java.util.regex.*;
  
  public class Solution 
  {
  
     public static void main(String[] args) 
    {
     Scanner sc = new Scanner(System.in);
     int t = Integer.parseInt(sc.nextLine());
      while((t--)&gt;0)
      {
          String s1 = sc.nextLine();
          String s2 = sc.nextLine();
          System.out.println(ms(s1,s2));
      }
    }
  
   public static String ms(String s1, String s2)
   {
       StringBuffer sb = new StringBuffer();
       int i,j;
       for(i=0,j=0;i&lt;s1.length() || j&lt;s2.length() ;)
       {
          if(s1.charAt(i)&gt;s2.charAt(j))
              {
                  sb.append(s2.charAt(j));
                  if(j==s2.length())
                      break;
                  else
                      j++;
              }
          else
              {
                  sb.append(s1.charAt(i));
                  if(i==s1.length())
                      break;
                  else
                      i++;
              }
       }
  
       if(j==s2.length())
       {  
       for(;i&lt;s1.length();i++)
              sb.append(s1.charAt(i));
  
       return sb.toString();
       }
  
       else
       {
       for(;j&lt;s2.length();j++)
              sb.append(s2.charAt(j));
  
        return sb.toString();
        }
     }   
   }
  </code></pre>
  
  <p>i am getting this error :</p>
  
  <pre><code>Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
  at java.lang.String.charAt(String.java:658)
  at Solution.ms(Solution.java:28)
  at Solution.main(Solution.java:18)
  </code></pre>
  
  <p>Can anyone please tell me where it went wrong? I couldn't find the error...</p>