Im trying to do this programme
int contains(String s1, String s2): receives two strings, and checks whether s2 is in s1 case sensitively. If s2 is in s1 it returns the index of the last occurrence of s2, otherwise it returns -1.
Here is an example
Enter first string: O pikap, su pikap, bu pikap.
Enter second string: pikap
22
Enter first string: O pikap, su pikap, bu pikap.
Enter second string: pikapcik
-1
I've written the codes but gave me an error like this
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(Unknown Source)
at Test.main(Test.java:12)**
Why there is such an error and how do i fix this? Im only allowed use charAt and length methods
Here is the code
public static void main(String[] args){
String b1 = "O pikap, su pikap, bu pikap";
String a1 = "pikap";
String sum = "";
int b =b1.length();
int a =a1.length();
for( ; b>0 ; b--){
b = b - a;
for(int n = 0; n < a ; n++ , b++){
sum+= b1.charAt(b);
}
if(sum == a1)
break;
}//for 1
System.out.println(b-a+1);
}