I want to read a data stream and everytime it reads a certain word or phrase I want the count to go up. The example I have below fails to count it. I tried looking for “echo percent” as well. All the bat file does is echo percent.
try {
String ls_str;
String percent = "percent";
Process ls_proc = Runtime.getRuntime().exec("c:\\temp\\percenttest.bat");
// get its output (your input) stream
DataInputStream ls_in = new DataInputStream(ls_proc.getInputStream());
while ((ls_str = ls_in.readLine()) != null ) {
System.out.println(ls_str);
progressBar.setValue(progress);
taskOutput.append(String.format(ls_str+"\n", progress));
if (ls_str == percent) {
progress++;
}
}
} catch (IOException e1) {
System.out.println(e1.toString());
e1.printStackTrace();
}
setProgress(Math.min(progress, 100));
Don’t compare the Strings with
==, use theequalsmethod.If you compare the Strings with
==, you’re checking to see if they’re the sameString.If you compare them with
equals, you’re checking whether or not their contents are the same.Instead of:
if (ls_str == percent)Do this:
if (ls_str.equals(percent))If you want to ignore case, you can do it like this:
if (ls_str.equalsIgnoreCase(percent))EDIT
Your String format is also messed up.
Change:
taskOutput.append(String.format(ls_str+"\n", progress));
to:
taskOutput.append(String.format(ls_str+"\n"), progress);
Notice the parentheses change.
Take a look at these for more explanations:
Java String.equals versus ==
http://www.java-samples.com/showtutorial.php?tutorialid=221