I need to make a program that prints the longest common sub-string out of two strings.
for example: 
String str1 = "abcdef";
String str2 = "abcgef";
the longest common string should be "abc".
I can only use loops, strings, and arrays! no methods/functions etc.. I'm a beginner and although I know functions I am not allowed to use it.
I tried using a count variable so that the last letter wont be compared to others chars from the second string over and over but the same error occurs.
String com = "";
String com2 = "";
int a;
int b;
for (i = 0; i < str1.length(); i++) {
    int count1 = 0;
    int count2 = 0;
    for (int j = 0; j < str2.length(); j++) {        
        a = i;
        b = j;
        com2 = "";
        while (str1.charAt(a) == str2.charAt(b)) {
            com2 = com2 + str1.charAt(a);                  
            if (com2.length()>com.length()) {              
                com = com2; 
            }     
            if (a<str1.length()-1) {       
                a++;  
            }
            if (b<str2.length()-1) {       
                b++;
            }                                   
        } 
    } 
} 
System.out.println(com);
like I said, the result should be "abc" and that's it, but I get a runtime error saying StringIndexOutOfBoundsException out of range 6.
thanks!
 
     
     
     
     
     
     
    