int comparison always returns true?

T

thomasjbs

I can't figure this out - new to java, but code appears to be ok.

I'm looking for a string within lines of text returned from a system
command. I get all the output back - no problem. But when I try to
do something based on indexOf, it always returns true:

Here is output from System.out.println statements showing the text
does *not* exist in the output (and another example showing it does)

Here is the code to search for it. On



int tf = 0;
boolean textfound = false;

// Loop through lines of text with Try and readLine...
{
tf = inputLine.indexOf("FINDTHISSTRING");
System.out.println(tf);

if ( tf > -1 );
{
textfound = true;
}
}


I also tried " tf == 0 ".

Example of Failure to find Text
======================

-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1


Example of case where FIND text Exists:
==============================

-1
-1
-1
-1
-1
-1
0 Found it here
-1
0 and Here
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
-1
 
T

thomasjbs

The semicolon after the parenthesis (on the first line) is the end of
the "if"-statement. The "{ textfound = true; }" block following it is
thus not affected by the conditional. It's just a stand-alone block.

- Logan

lol

I work in so many different languages with different syntax that I
didn't even notice it.

I skipped over it about 50 times. Its so obvious...now. I think the
braces give my single, unconditional assignment a nice emphasis.

Thanks for your help. It worked perfectly after removing the
semicolon.
 
A

Arne Vajhøj

I work in so many different languages with different syntax that I
didn't even notice it.

There are many languages where the same problem is possible.

Arne
 
R

Roedy Green

I can't figure this out - new to java, but code appears to be ok.

I'm looking for a string within lines of text returned from a system
command. I get all the output back - no problem. But when I try to
do something based on indexOf, it always returns true:

Here is output from System.out.println statements showing the text
does *not* exist in the output (and another example showing it does)

Here is the code to search for it. On
I would write that more tersely as:

final boolean found = inputLine.indexOf("FINDTHISSTRING") >= 0;
 
R

Roedy Green

int tf = 0;
boolean textfound = false;

// Loop through lines of text with Try and readLine...
{
tf = inputLine.indexOf("FINDTHISSTRING");
System.out.println(tf);

if ( tf > -1 );
{
textfound = true;
}
}

You have not show the loop just hinted there is one. But I suspect
what you did was set textFound false only once. Once it goes true, it
stays true.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top