2

Hello I have a programme where you type in different commands. The only command I have at the moment is ping command. You type ping followed by your IP address.

example: ping 192.167.2.1

I want to get a result from this ping so I do this:

String ip = ""+l6+l7+l8+l9+l10+l11+l12+l13+l14+l15+l16;
String pingResult = "";

String pingCmd = "ping " + ip;

try {
    Runtime r = Runtime.getRuntime();
    Process p = r.exec(pingCmd);

    BufferedReader in = new BufferedReader(new
    InputStreamReader(p.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        System.out.println(inputLine);
        pingResult += inputLine;
    }

    in.close();

} catch (IOException e) {
    System.out.println(e);
}

When I press a key the key value is stored in a string.

If l1 is equal to null, then the value gets stored in l1,
if l1 is not null and l2 is null then the next key that is pressed is stored in l2 and so on.

The maximum amount of keys you can press is 20. when you press enter all the strings are put together to form one big string. I want to be able to split that big string and take away the letters and leave the numbers. How can I do this.

2
  • 1
    I think it would be helpful if you provided an example of an output as you seek it Commented Jul 24, 2015 at 22:21
  • Why bother trying to validate your ping command? If you pass a bad ping command to your Process, then p.getErrorStream() would have information for you. Commented Jul 25, 2015 at 2:06

2 Answers 2

1

This will replace all alphabets and leave only numbers :

ip = ip.replaceAll("[a-zA-Z]", "");
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest using this :

ip = ip.replaceAll("[\\D&&[^\\.]]", "");

It will remove every non digit character except '.' which I believe you will need to form a correct IP ( something that you missed in the description).

1 Comment

In other words replaceAll("[^\\d.]","")?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.