indexOf issues

I have a section of code here:

            String responseLine;
            while ((responseLine = tc.is.readLine()) != null) {
                System.out.println(responseLine);
                if (responseLine.indexOf("Hot Water Temperature") == 1) {
                    ret = true;
                    break;
                } else if (responseLine.indexOf("Hot Water Temperature") == 0) {
                    ret = false;
                    break;
                }
            }

The issue being, that when I run this, it returns true.

Now, if i take the else if off and just leave else, hoping that if it doesn't find the text it will return false it doesn't work.

I tried changing it to != -1 return true else false, this is the only way it returns true.

what am I doing wrong here?

Thanks,

LVL 2
k41d3nAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Initialize ret to false

ret = false;

before looping
0
CEHJCommented:
Also, for not found it should be

else if (responseLine.indexOf("Hot Water Temperature") < 0) {
0
CEHJCommented:
You can simplify thus:

boolean found = false;
String responseLine = null;
while ((responseLine = tc.is.readLine()) != null && found == false) {
      System.out.println(responseLine);
      found =  (responseLine.indexOf("Hot Water Temperature") > -1);
}
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

k41d3nAuthor Commented:
Ok CEHJ, that last block works for me, except, if it doesn't find the context then it just hangs and doesn't progress.

here is the full method:

  public static boolean checkHotWaterTemp() {
        try {
            telnetFunctions tf = new telnetFunctions();
            telnetConnect tc = new telnetConnect();
            TestLog log = new TestLog();
           
            tc.makeConnect();
            tc.outputStream();
            tc.inputStream();
            if (tc.telnetSocket != null && tc.os != null && tc.is != null) {
                tf.sendLogin(tc.os, "apc", "apc");
                tf.setMenu(tc.os, tc.is, numberOfModules.CONTEXT_DEVMANAGER);
                tf.setMenu(tc.os, tc.is, numberOfModules.CONTEXT_SYSTEM);
                tf.setMenu(tc.os, tc.is, numberOfModules.CONTEXT_GENERAL);
                tf.setMenu(tc.os, tc.is, "Status");
            }

            while ((responseLine = tc.is.readLine()) != null && found == false) {
                System.out.println(responseLine);
                found =  (responseLine.indexOf("Hot Water Temperature") > -1);
                break;
            }
            tc.telnetClose();
        } catch (IOException e) {
            System.out.println(e);
        } catch (Exception ge) {
            System.out.println("General Exception caught in ExceptionExample.compute().  Message: " + ge.getMessage());
            ge.printStackTrace();
        }
        return(found);
    }


boolean found = false;
String responseLine = null;
 are both outlined at the beginning of the class.

I appreciate your help, I am still learning the ropes of java.
0
CEHJCommented:
The hanging is another issue entirely. The call to readLine will block until further data is available to be read

You don't need the break statement btw
0
k41d3nAuthor Commented:
yeah, I took the break out. I just don't understand whats going on.
0
CEHJCommented:
What's sending has two options:

a. to close the stream on which it's sending - your loop will exit
b. to send an end of input (output?) marker
0
CEHJCommented:
End of output ;-)
0
k41d3nAuthor Commented:
How do I mark that it is end of output?
0
CEHJCommented:
The sender will have to send something pre-arranged, such as

"EOF"

or anything. Doesn't matter really. Is that a *real* telnet server btw?
0
k41d3nAuthor Commented:
if by REAL you mean microsoft, no. It's for a Network Management card that has built in telnet.

basically I send it commands via a socket connection and read to make sure that the commands are accepted etc;

I think the issue I am having, is that without an

} else if (responseLine.indexOf("Hot Water Temperature") == 0) {
          ret = false;
}

it isn't reading it all the way and I don't know why. it should be printing it out, and reading it at the same time. and once it finds the "Hot Water Temperature" in the line it returns true and continues on.

if I take out the "else if" statement and just leave it as else { then it doesn't read it all the way, but with the else if statement I can see that it's printing the page where it should find "Hot Water Temperature"

I should also note, that if I set the telnet program to show "Hot Water Temperature" it will return true, but when I set it to not show "Hot water temperature" then it hangs where is hould find it.

I hope this makes sense, because it doesn't to me ;)
0
CEHJCommented:
You have to read all the input if the app needs to continue. Just flag it as found and then continue reading come what may
0
CEHJCommented:
But according to the code above it seems that you *don't* want to continue - i'm confused ;-)
0
CEHJCommented:
So if you *do* need to continue:

boolean found = false;
String responseLine = null;
while ((responseLine = tc.is.readLine()) != null) {
      System.out.println(responseLine);
      found =  (responseLine.indexOf("Hot Water Temperature") > -1);
      if (found) {
            System.out.println("Found temperature");
      }
}
0
CEHJCommented:
To summarize:

a. if you have a reason for reading on, just flag it and keep reading (the server will have to either close the socket or send an 'eof'
b. if you just want to return after find hot water, stop reading and close
0
k41d3nAuthor Commented:
The problem, is that if it doesn't find hot water it hangs, the only way I know to stop reading is to kill the output stream, but if I do that, then it throws an error.
0
k41d3nAuthor Commented:
if I do not find hot water then I need it to end and move on. if it dies find it then I just need it to return true, which it does.

It just isn't ending if it doesn't find it.
0
CEHJCommented:
The options to stop the hanging are as i mentioned above. A third is to use non-blocking io
0
CEHJCommented:
>>It just isn't ending if it doesn't find it.

It doesn't end because it blocks.

>>is to kill the output stream, but if I do that, then it throws an error.

What do error you get btw?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
k41d3nAuthor Commented:
Got it with your help, thank you!
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.