Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to extract IP addresses from a single long String in Java
    primarykey
    data
    text
    <p>I am writing an android app to do a traceroute command.</p> <p>Right now when the traceroute executes it puts the console output into a very long single string. This is done by the following code:</p> <pre><code> public void runAsRoot(String[] cmds) throws Exception { Process p = Runtime.getRuntime().exec("su"); DataOutputStream os = new DataOutputStream(p.getOutputStream()); InputStream is = p.getInputStream(); for (String tmpCmd : cmds) { os.writeBytes(tmpCmd+"\n"); int readed = 0; byte[] buff = new byte[4096]; // if cmd requires an output // due to the blocking behaviour of read(...) boolean cmdRequiresAnOutput = true; if (cmdRequiresAnOutput) { while( is.available() &lt;= 0) { try { Thread.sleep(10000); } catch(Exception ex) {} //timeout.. gotta watch this carefully. } while( is.available() &gt; 0) { readed = is.read(buff); if ( readed &lt;= 0 ) break; String seg = new String(buff,0,readed); System.out.println("#&gt; "+seg); ListofIPs = seg; } } } os.writeBytes("exit\n"); os.flush(); } </code></pre> <p>The output of this strings looks like this: <img src="https://i.stack.imgur.com/XZMeg.png" alt="logcat output after printing the string"></p> <p>What I want to do is take this output and extract only the IP addresses and put them in order into an array. This is where I am at a loss of even where to begin. I'm thinking some type of String manipulation but don't know where to start.</p> <p>If anyone has any idea or pointers I would greatly appreciate it.</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload