-2

I am trying to solve this question: https://www.hackerrank.com/challenges/morgan-and-a-string

Here's my 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--)>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<s1.length() || j<s2.length() ;)
     {
        if(s1.charAt(i)>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<s1.length();i++)
            sb.append(s1.charAt(i));

     return sb.toString();
     }

     else
     {
     for(;j<s2.length();j++)
            sb.append(s2.charAt(j));

      return sb.toString();
      }
   }   
 }

i am getting this error :

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)

Can anyone please tell me where it went wrong? I couldn't find the error...

coder101
  • 770
  • 2
  • 9
  • 25
  • http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – assylias May 11 '15 at 07:52
  • 3
  • Side note: such problems can often be resolved by just running the program in a debugger; or (less "recommended": by inserting trace statements for each important statement.) – GhostCat May 11 '15 at 07:58

2 Answers2

3

i dont know that you are trying to do with the program but i think this will eliminate your error

for(i=0,j=0;i<s1.length() & j<s2.length() ;)
2

I have just altered your for loop as below and I could able to execute the program without any issues

for(i = 0, j = 0; i < s1.length() && j < s2.length();)

PFB the source code

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int t = Integer.parseInt(sc.nextLine());
    while ((t--) > 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 < s1.length() && j < s2.length();) {
        if (s1.charAt(i) > 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 < s1.length(); i++)
            sb.append(s1.charAt(i));

        return sb.toString();
    }

    else {
        for (; j < s2.length(); j++)
            sb.append(s2.charAt(j));

        return sb.toString();
    }
}
}
activesince93
  • 1,651
  • 15
  • 36
Thomas
  • 477
  • 4
  • 16