int comparison always returns true?

Discussion in 'Java' started by thomasjbs@gmail.com, Jun 8, 2008.

  1. Guest

    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
    , Jun 8, 2008
    #1
    1. Advertising

  2. Guest

    On Jun 8, 3:07 pm, Logan Shaw <> wrote:
    > wrote:
    > > if ( tf > -1 );
    > > {
    > > textfound = true;
    > > }
    > > }

    >
    > 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.
    , Jun 8, 2008
    #2
    1. Advertising

  3. Arne Vajhøj Guest

    wrote:
    > On Jun 8, 3:07 pm, Logan Shaw <> wrote:
    >> wrote:
    >>> if ( tf > -1 );
    >>> {
    >>> textfound = true;
    >>> }
    >>> }

    >> 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.


    > 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
    Arne Vajhøj, Jun 8, 2008
    #3
  4. Roedy Green Guest

    On Sun, 8 Jun 2008 14:56:28 -0700 (PDT), ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    >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;
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 9, 2008
    #4
  5. Roedy Green Guest

    On Sun, 8 Jun 2008 14:56:28 -0700 (PDT), ""
    <> wrote, quoted or indirectly quoted someone who
    said :

    >
    >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.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 9, 2008
    #5
  6. Roedy Green Guest

    On Sun, 8 Jun 2008 14:56:28 -0700 (PDT), ""
    <> wrote, quoted or indirectly quoted someone who
    said :

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


    If you ran that through a lint in your IDE it would warn you of the
    empty if-true clause.


    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Jun 9, 2008
    #6
  7. Arne Vajhøj wrote:
    > wrote:
    >> On Jun 8, 3:07 pm, Logan Shaw <> wrote:
    >>> wrote:
    >>>> if ( tf > -1 );
    >>>> {
    >>>> textfound = true;
    >>>> }
    >>>> }
    >>> 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.

    >
    >> 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.


    All the C-based ones, at least.
    Mike Schilling, Jun 9, 2008
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,206
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,626
    Old Wolf
    Jan 20, 2004
  3. Pierre Quentel

    "0 in [True,False]" returns True

    Pierre Quentel, Dec 12, 2005, in forum: Python
    Replies:
    59
    Views:
    1,028
    Grant Edwards
    Dec 16, 2005
  4. bdb112
    Replies:
    45
    Views:
    1,334
    jazbees
    Apr 29, 2009
  5. Replies:
    6
    Views:
    1,357
    Peter Nilsson
    Oct 26, 2009
Loading...

Share This Page