pythonic_love
pythonic_love

Reputation: 73

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range:

My code is:

import java.util.*;
public class Lab4Exercise {

public static String reverseString(String a){
    int x = a.length();
    String b = "";
    while(x > 0){
        int y = x - 1;
        b = b + a.charAt(y);
        x--;
    }
    return b;
}
public static String encrypt(String a){
    String a1 = a.replaceAll("\\p{Punct}+", "");
    String a2 = a1.replaceAll(" ", "");
    String str = "";
    String str2 = "";
    if(a.length() % 2 == 0){
        for(int q = 0; q <= a2.length(); q += 2){
            str = str + a.charAt(q);
    }
        for(int w = 1; w < a2.length(); w += 2){
            str2 = str2 + a.charAt(w);
    }
        return str.concat(str2);
}
    else{
        for(int z = 0; z < a2.length(); z += 2){
            str2 = str2 + a.charAt(z);
        }
        for(int c = 1; c <= a2.length(); c += 2){
            str2 = str2 + a.charAt(c);
        }
        return str.concat(str2);
    }
}
public static int countTool(String a){
    a = a.toLowerCase();
    int g = a.length();
    String str = "";        
    while(g >= 0){
        if(a.charAt(g) == 'a'){
            str = str + a.charAt(g);
        }
        g--;            
    }
    int h = str.length();
    return h;
}
public static double Equation(int n, int x){
    double a = 0, t = 0;
    if(n >= 0){
        double w = 0.0;
        for(int k = 0; k <= n; k++){
            a = Math.pow(x, k)*Math.pow(Math.E, (n - k));
            w = w + a;}
        return Math.abs(w);
    }
    else {
        double q = 0;
        for(int b = 0; b >= n; b--){
            t = Math.pow(x, b)*Math.pow(Math.E, (n - b));
            q = q + t;}
        return Math.abs(q);
    }
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.println("Enter case number: ");
    int caseVal = scan.nextInt();
    switch(caseVal){

    case 1:
        System.out.println("Enter a string to reverse: ");
        String str = scan.next();
        System.out.println("Reversed string is: " + reverseString(str));        
        break;

    case 2:
        System.out.println("Enter a string to encrypt");
        String str2 = scan.next();
        System.out.println("Encrypted text is: " + encrypt(str2));
        break;

    case 3:
        System.out.println("Enter a string to count: ");
        String str3 = scan.next();
        System.out.println(countTool(str3));
        break;

    case 4:
        System.out.println("Enter n value: ");
        int n = scan.nextInt();
        System.out.println("Enter x value: ");
        int x = scan.nextInt();
        System.out.println(Equation(n, x)); 
        break;

    case 5:
        Random random = new Random();
        int randomnum = 0;
        for(int p = 0; p < 50; p++){
            randomnum = 20 + (int)(Math.random()*119);
            System.out.println(Math.sqrt(randomnum));
        }
    }
}

}



So when I try to execute case 2 and case 3 it gives this error:(for example I selected "I love trees!" in both cases)

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
    at java.lang.String.charAt(Unknown Source)
    at Lab4Exercise.encrypt(Lab4Exercise.java:33)
    at Lab4Exercise.main(Lab4Exercise.java:84)

For the case 3:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
        at java.lang.String.charAt(Unknown Source)<br/>
        at Lab4Exercise.countTool(Lab4Exercise.java:43)<br/>
        at Lab4Exercise.main(Lab4Exercise.java:90)<br/>

Upvotes: 0

Views: 338

Answers (1)

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

Line no 33, you are trying to access the array at the index which doesn't exist.

change

for(int c = 1; c <= a2.length(); c += 2){

to

 for(int c = 1; c <a2.length(); c += 2){

Be careful while playing with the size of the string. If the size of string is 8 that means you can access it from index 0 till 7 and yes, run the code in DEBUG mode and keep checking the values, that would really help you out.

Upvotes: 1

Related Questions