如何打印输出循环java的值

时间:2015-10-23 13:42:24

标签: java string loops setter getter

任何人都可以帮助我吗? 我有代码:

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

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

结果是

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

但我希望得到这样的东西

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

3 个答案:

答案 0 :(得分:0)

lineValue=line;会在while循环的每次迭代中不断重置lineValue的值

而是使用lineValue += "\n" + linelineValue = lineValue + "\n" + line插入换行符并附加到lineValue而不是设置lineValue

答案 1 :(得分:0)

使用String[] lineValue;代替String lineValue,因为只有最后一行被分配给此变量并且

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

也改变了,

public String[] getLineValue() {
     for(int i = 0; i < lineValue.length; i++) {
          System.out.println(lineValue[i]);
     }
     return lineValue;   // array return
}

答案 2 :(得分:0)

我使用多线程

完成了它
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;}}

结束主要课程

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

}}