i coded for negative numbers ,but i don’t know why it sho ws array index out of bound exception !
can you help me in this , following is my code…
public class II_string_to_int {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
System.out.println("enter the string");
String str = s.next();
string_to_int(str, 0, 0);
}
public static void string_to_int(String str, int si, int ans) {
if(str.charAt(0)=='-') {
si++;
}
if (si == str.length()) {
if (str.charAt(0) == '-') {
ans=-ans;
System.out.println(ans);
}
else {
System.out.println(ans);
}
return;
}
ans=ans*10;
ans=ans+(str.charAt(si)-'0');
string_to_int(str, si+1, ans);
}
}