-1

Why isn't this code working? What I'm trying to do is create a ReplaceAll() method but it is giving ArrayIndexOutOfBoundsException error.

This is Class file:

public class MyString{
private char[] data;
public MyString(){
}
public MyString(String s){
    data = s.toCharArray();
}
public char replaceFirst(char o, char n){
    for(int i=0; i<data.length; i++){
        if(data[i]==o){
            data[i]=n;
        }
    }
    return data[n];
}

and this is my tester file:

public class Tester{
    public static void main(String[] args){
        MyString m1 = new MyString();
        MyString m2 = new MyString("Nafees");
        System.out.println(m2.replaceFirst('N','k'));
    }
}

Thanks in advance. And if I made any mistake while asking this question, sorry.

rghome
  • 8,529
  • 8
  • 43
  • 62
Sohan
  • 23
  • 1
  • 7
  • I see no `replaceAll` method. Also in your `replaceFirst` method `if(data[i]==0)` should probably be `if(data[i]==o)` – GBlodgett Mar 14 '19 at 14:48
  • 3
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – rghome Mar 14 '19 at 14:49
  • 1
    Please see the linked answer. The community doesn't like to debug these exceptions since the cause is usually a basic programming error. – rghome Mar 14 '19 at 14:50
  • 2
    The `replaceFirst` method should probably return `return n;` as `n` might be higher than the `data.length` hence the `IndexOutOfBoundException` –  Mar 14 '19 at 14:50
  • 1
    You are returning data[n] where n is converted to int. – bhusak Mar 14 '19 at 14:50
  • there should be data.length -1, cause last index is 1 less than length – Taavi Kivimaa Mar 14 '19 at 14:53
  • 1
    @TaaviKivimaa No it shouldn't, because OP is using `<` – GBlodgett Mar 14 '19 at 14:54

3 Answers3

2

Problem is in the last line of the method:

 return data[n];

n is the replacement character, but java interprets it as a number here. The n is a character "k", which is also number 107. And your input string "Nafees" doesn't have so many characters.

If you want to get the String with replaced characters, you should use this instead:

return new String(data);

P.S. You should rename the method or change implementation, because you are replacing all matching characters, not first.

NeplatnyUdaj
  • 6,052
  • 6
  • 43
  • 76
0

You return at the end of the method data[n]. However n is the parameter (a char) of your method and not an integer. So He will convert n as an integer which will be out of the limit of your array of character.

Bazouk55555
  • 557
  • 6
  • 24
0

Whilst it's great that everyone is helping diagnose and debug your code, I'd like to point out that replaceFirst is part of the standard Java String object since Java 1.4, although it does expect a regex and string instead of two char values.

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replaceFirst(java.lang.String,%20java.lang.String)

There is also a replaceAll method on String, if that was the intended behaviour.

antonyh
  • 2,131
  • 2
  • 21
  • 42