1

I am having trouble with my code. as i run the program it displays array index out of bound exception.Please suggest what are the errors in the code.

package replacespace;

import java.util.Scanner;



public class ReplaceSpace {

   /*public  int calLength(char par1[]){
       int count=0;
       for(char c:par1){
            count++;
       }
       return(count);
   }*/

    /**
     *
     * @param args
     */
    public void replaceSpace(char str[], int length){
         char[] newArray= new char[length+1];
         newArray[length+1]='\0';
         int position=0;
         for(int i=0;i<length;i++){
             if(str[i]==' '){
                 length=length+2;
                 newArray[position]='%';
                 newArray[position+1]='2';
                 newArray[position+2]='0';
                 position=position+3;

             }
             else{
                 newArray[position]=str[i];
                 position++;
             }
         }
         String s=new String(newArray);
             System.out.println(s);
    }

    public static void main(String[] args) {
        ReplaceSpace rs= new ReplaceSpace();
        String s1,sCopy;

       int length;
       Scanner scan= new Scanner(System.in);
       System.out.println("Please enter any string");
       s1=scan.nextLine();
       length=s1.length();
       char stringArr[]=new char[length];
       stringArr =s1.toCharArray();

       //length=rs.calLength(stringArr);

        rs.replaceSpace(stringArr,stringArr.length);
   }

}

this code is suppose to replace space in string with '%20'.

  • 2
    `string.replace(" ", "%20")`? – Andy Turner Jul 27 '16 at 12:29
  • 1
    This code is highly confusing. Consider not using names like array, and StringArr1, s1 and so on for your variables. Gives things names that say what they are. You see, this code is simply written in such a complicated way that understanding its intent ... is like a major task. Seriously: focus on writing code that is easy to read. You know: when your code is easy to read, you will also find it much easier to find bugs in your code. As starter, you might want to look into "CleanCode" by Robert Martin. – GhostCat Jul 27 '16 at 12:30
  • 1
    Why is it surprising that you're getting `ArrayIndexOutOfBoundsException`? You increase the value of the integer `length`, but that does not increase the actual length of `stringArr1`, and then you try to go as far as 5 past the end of the array (`length = length + 2` and looping until `j <= length`, indexing at `j + 3`) – jonhopkins Jul 27 '16 at 12:33
  • I can't use inbuilt methods such as replace –  Jul 28 '16 at 04:23

1 Answers1

2
for(int j=var1;j<=length;j++){
                   stringArr1[j]= '%';
                  stringArr1[j+1]='2';
                  stringArr1[i+2]='0';
                   stringArr1[j+3]=array[i+1];
               }

This code runs until j is equal to length. you can not have [j+3] index for that iteration (or the one(s)) before.

Change your j<=length to j<=(length-3) (or similar)

But my recommendation: use Strings replace or replaceAll method

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • 1
    Or use a `StringBuilder`. – Andy Turner Jul 27 '16 at 12:34
  • 1
    It should be noted that `length - 3` isn't enough. `length` is increased by 2 before every time that loop is run – jonhopkins Jul 27 '16 at 12:35
  • @jonhopkins indeed. I just checked the indices as being 'will be augmented'. I wasn't sure by how much they were augmented which is why I set the (or similar). – Stultuske Jul 27 '16 at 12:40
  • My bad, I didn't see the (or similar). But in the end, it should just be `j < length` with the actual array being reallocated with the additional length before the loop. For the purposes of this exercise anyway. Really your recommendation for `String`'s replacement methods is the best option. – jonhopkins Jul 27 '16 at 12:52
  • Still showing same error –  Jul 27 '16 at 13:59
  • what (exactly) did you change, and what does the complete stacktrace say? – Stultuske Jul 28 '16 at 09:08