rishiag
rishiag

Reputation: 2274

Why is new line getting appended to my array element when I am trying to print this?

private int Index(String[] match,String keyword){
   int m=0;
   keyword=keyword+"1";
   match[m]=match[m]+"1";
   System.out.println("match:"+match[m]);
   System.out.println("keyword:"+keyword);
   System.out.println(match[m].equals(keyword));
   while(!(match[m].equals("")) && !(match[m].equals(null))){
           System.out.println("yes");
       if(match[m].equals(keyword)){
         break;
       }
       else
           m++;
   }
   return m;
}

And I am getting following output (value of keyword is sparktg):

match:sparktg
1
keyword:sparktg1
false

Why in the case of match[m], there is a new line between "sparktg" & "1"?

Upvotes: 0

Views: 159

Answers (4)

alaster
alaster

Reputation: 4171

That is not the answer, but notation about the code

match[m].equals(null) will throw an NullPointerException. The right way to check if match[m] not equals null is: mathc[m] != null before calling any method of your object. So use this:

match[m] != null && !match[m].equals("")

instead of this:

!match[m].equals("") && !match[m].equals(null)

Upvotes: 0

amicngh
amicngh

Reputation: 7899

Try this. Replace all the new line before parsing.

private static int Index(String[] match,String keyword){
       int m=0;

       for(int k=0;k<match.length;k++){
           if(match[k]!=null)
           match[k]= match[k].replace("\n", "");

       }
       if(keyword!=null)
           keyword= keyword.replace("\n", "");

       keyword=keyword+"1";
       match[m]=match[m]+"1";
       System.out.println("match:"+match[m]);
       System.out.println("keyword:"+keyword);
       System.out.println(match[m].equals(keyword));
       while(!(match[m].equals("")) && !(match[m].equals(null))){
               System.out.println("yes");
           if(match[m].equals(keyword)){
             break;
           }
           else
               m++;
       }
       return m;
    }

Upvotes: 0

Pieter M&#252;ller
Pieter M&#252;ller

Reputation: 4693

The only reason I can see is that match[0] already ends in a newline. You should check by outputting match[0] before adding the "1". A good practice is to output in this form:

System.out.println("|"+match[0]+"|");

...thus using the | to clearly mark where your string starts and ends.

You can use trim() to cut off any whitespace, including newlines:

match[m] = match[m].trim() + "1";

However, this will also remove spaces and tabs, which may or may not be a problem for you. When I compare strings, I often trim both strings first, just to be safe, but only if you are disregarding whitespace.

Upvotes: 1

adarshr
adarshr

Reputation: 62603

If you have no control over the input, you can do a trim() before you use the inputs. This eliminates any \n and spaces.

if(match[m] != null) {
   System.out.println("match:"+match[m].trim());
}
if(keyword != null) {
   System.out.println("keyword:"+keyword.trim());
}

You can make it cleaner by writing a utility method to do this.

public String sanitize(String input) {
    return input != null ? input.trim() : null;
}

and use it as so:

match[m] = sanitize(match[m]);
keyword = sanitize(keyword);

Upvotes: 2

Related Questions