so basically, a user inputs 2 strings ( CATSATONTHEMAT AT ) and we need to count how many time the second string appears in the first string ( so the answer here is 3 )
this is what I have so far, and it keeps saying
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 81223 at java.lang.String.substring(Unknown Source) at practice.main(practice.java:60)"
any help would be appreciated! I just can't see to find where I went wrong
String s = scan.next(); // CATSATONTHEMAT
String t = scan.next(); // AT
int j= 0;
for ( int i = 0 ; i < s.length(); i++){
int k = t.length();
String newstring = s.substring(i,i+k); // I printed this and the substring works so the if statement might not be working..
if(newstring.equals(t))
j++; // if the new substring equal "AT" then add 1
}
System.out.printf("%d", j); // suppose to print just 3
String.indexOf(String str, int fromIndex)? In the real world you'd just use StringUtils.countMatches() from commons-lang!