• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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,

0
k41d3n
Asked:
k41d3n
  • 14
  • 7
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
k41d3nAuthor Commented:
Got it with your help, thank you!
0
 
CEHJCommented:
8-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 14
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now