0

can anybody help me? I have code:

    public class Demo {
         String lineValue;
    public  void execute () {String a[] = new String[] { "ping google.com", "ping youtube.com" };

    for (int i = 0; i < a.length; i++) {

        try {
            Process process = Runtime.getRuntime().exec(a[i]);

            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
         //  System.out.println(line);
          lineValue=line;
            }

            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
public String getLineValue() {
     System.out.println(lineValue);
return lineValue;
}

and

public class Test {
String result;
public static void main(String[] args) {
Test t = new Test();
Demo d = new Demo();
d.execute();
d.getLineValue();
}
}

the result is

Minimum = 23ms, Maximum = 29ms, Average = 25ms

but i want get something like this

Pinging google.com [216.58.209.206] with 32 bytes of data:
Reply from 216.58.209.206: bytes=32 time=23ms TTL=56
Reply from 216.58.209.206: bytes=32 time=23ms TTL=56
Request timed out.
Reply from 216.58.209.206: bytes=32 time=24ms TTL=56

Ping statistics for 216.58.209.206:
    Packets: Sent = 4, Received = 3, Lost = 1 (25% loss),
Approximate round trip times in milli-seconds:
    Minimum = 23ms, Maximum = 24ms, Average = 23ms

Pinging youtube.com [216.58.209.174] with 32 bytes of data:
Reply from 216.58.209.174: bytes=32 time=24ms TTL=55
Reply from 216.58.209.174: bytes=32 time=24ms TTL=55
Reply from 216.58.209.174: bytes=32 time=23ms TTL=55
Reply from 216.58.209.174: bytes=32 time=29ms TTL=55

Ping statistics for 216.58.209.174:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 23ms, Maximum = 29ms, Average = 25ms
Orion
  • 59
  • 2
  • 10
  • for example: if I will be ping 1000 sites, I will see result after few minutes, goal is see result dynamically, step by step. Not all result in the end iteration – Orion Oct 23 '15 at 14:06
  • You can remove `Test t = new Test();` from your main method, it is serving no purpose currently – phflack Oct 23 '15 at 14:08
  • yes, I don't use object t I forgot to delete it – Orion Oct 23 '15 at 14:13

3 Answers3

0

lineValue=line; constantly resets the value of lineValue each iteration of your while loop

Instead use lineValue += "\n" + line or lineValue = lineValue + "\n" + line to insert new line characters and append to lineValue instead of setting lineValue

phflack
  • 2,729
  • 1
  • 9
  • 21
  • yes, but for example: if I will be ping 1000 sites, I will see result after few minutes, goal is see result dynamically, step by step. Not all result in the end iteration – Orion Oct 23 '15 at 13:59
  • In that case, put a `System.out.println(lineValue);` at the end of your for loop and then use `lineValue = ""` right after – phflack Oct 23 '15 at 14:09
0

Use String[] lineValue; instead of String lineValue because only last line is assigned to this variable and

int k = 0;
while ((line = reader.readLine()) != null) {
        lineValue[k]=line;
        k++;
} 

also change,

public String[] getLineValue() {
     for(int i = 0; i < lineValue.length; i++) {
          System.out.println(lineValue[i]);
     }
     return lineValue;   // array return
}
ccc
  • 370
  • 5
  • 19
  • And then hope that the number of lines returned by the ping or process called doesn't change, or else you'll go out of bounds – phflack Oct 23 '15 at 14:10
  • do like this: ArrayList lineList = new ArrayList(); ..... while ((line = reader.readLine()) != null) { if (!line.equals(null)) lineList.add(line); ...... } public ArrayList getLineValue() { for(String s:lineList) System.out.println(s); return lineList; } } see result after 10 seconds. its bad. – Orion Oct 23 '15 at 14:43
  • I see result after all iterations – Orion Oct 23 '15 at 16:51
0

I done It using Multithreading

public class Demo {
static String lineValue;
static boolean end = false;

public synchronized void execute() throws InterruptedException {

    String a[] = new String[] { "help", "help", "ping google.com" };
    for (int i = 0; i < a.length; i++) {
        try {
            Process process = Runtime.getRuntime().exec(a[i]);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.equals(null))
                    lineValue = line;
                setS(lineValue);
                wait();
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    end = true;
}

public static void setS(String lineValue) {

}

public synchronized String getLineValue() throws InterruptedException {
    notifyAll();
    if (!lineValue.isEmpty()) {
        System.out.println(lineValue);
    lineValue = "";
    }
    return lineValue;}}

end main class

public class Test {
static Demo sync = new Demo();

public static void main(String[] args) throws InterruptedException {

    new Thread(new Runnable() {
        public void run() {
            try {
                sync.execute();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();

    new Thread(new Runnable() {
        public void run() {
            for (;;) {
                try {
                    if (Demo.end==false) {
                        sync.getLineValue();
                    }
                    else
                    {
                        return;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}}
Orion
  • 59
  • 2
  • 10