user3929251
user3929251

Reputation: 173

Java Split string to numbers

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.

Upvotes: 2

Views: 85

Answers (2)

ap0calypt1c
ap0calypt1c

Reputation: 481

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).

Upvotes: 0

Samrat Dutta
Samrat Dutta

Reputation: 1737

This will replace all alphabets and leave only numbers :

ip = ip.replaceAll("[a-zA-Z]", "");

Upvotes: 1

Related Questions