1

I have following problem with java.This method in Class should return String as is.

private String getAsString(Resource res) {

        return "We wish you good luck in this exam!\nWe hope you are well pre-\npared.";
    }

Then in Constructor this String shlud be converted into array of words

private int index;
private String string_arr[];

public TextFileIterator(Resource res) {
    this.index=0;
    if(res==null){
        throw new NullPointerException();
    }
    String text=this.getAsString(res);
    //text=text.replaceAll("-\n(?=[a-z])", "");
    text=text.replaceAll("\\n", "");
    text=text.replaceAll("!", " ");
    text=text.replaceAll("-", "");
    text=text.replaceAll(".", "");
    this.string_arr=text.split(" ");

}

Problem is that at the end I get array which is null... what is the problem. I attach the debugger screenshots. enter image description here enter image description here enter image description here enter image description here

Please could explain me why does it happen?

Fedo
  • 349
  • 1
  • 7

1 Answers1

3

The culprit is line no 17-

text=text.replaceAll(".", "");

The above line is replacing all of the content with "", because in regex world "." means any character.

Try this instead-

text=text.replaceAll("\\.", "");
codeNdebug
  • 249
  • 1
  • 7