lorenz
lorenz

Reputation: 13

Find a substring and evaluate an expression

I'm working on something that is probably java basics but I still do not understand even after reading some documentation.

I have a csv file that i read into a sTokens variable row by row. At each row I need to check the value in element [0] and compare it with what I need to be there.

I've an array of strings like this:

String [] sTokens = {"elem1", "elem2"};

The following does not work as expected:

if (sTokens[0].indexOf("texttofind".toLowerCase())>=10) {
   //do something
}

My expectation would be to have the evaluated expression return a number (in this case 10 or in worst case -1) and then do something based on the number returned e.g. 10 means I found what I needed.

I'm using the debug tool in Netbeans and when I evaluate the expression in parenthesis it returns 10 (like it is supposed to do).

Unfortunately, and here is probably my mistake, the expression is evaluated as NULL.

Any help would be very appreciated lorenz

Upvotes: 0

Views: 107

Answers (2)

Kars
Kars

Reputation: 917

You are using indexOf wrongly. If this is what you have:

String [] sTokens = {"elem1", "elem2"};

And you run your code like this:

System.out.println(sTokens[0].indexOf("el".toLowerCase()));

It will print 0, because in sTokens[0] contains "el" starting at index 0.

If you run this:

System.out.println(sTokens[0].indexOf("em".toLowerCase()));

It will print 2, because "em" starts at index 2.

Upvotes: 1

NivedhaLak
NivedhaLak

Reputation: 249

I assume the sTokens might be in uppercase.Try doing this

String[] sTokens = {"Hello","TestC"};
if(sTokens [0].toLowerCase().indexOf("llo")>=2){
     //do something
}

Upvotes: 0

Related Questions