Ryan Jones
Ryan Jones

Reputation: 447

Split a String in Java

I am using the Java exec command to issue a "hcitool scan" command in order to perform a Bluetooth Scan.

The output is in the exact same format as it would be if i were to type in the command to the terminal

scanning...
mac address      bluetoothName
Done

I want to be able to split the returned string down, so that i can store the found MAC Addresses as a String.

My code so far is as follows:

import java.io.*;

public class altBluetooth
{
  public static void main(String args[])
  {
    try
    {
      Process p=Runtime.getRuntime().exec("hcitool scan");
      p.waitFor();
      BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
      String line=reader.readLine();

      while(line!=null)
      {
        System.out.println(line);
        line=reader.readLine();
      }
    }
    catch(IOException e1) {
      System.out.print("This didnt work - exception 1");
    }
    catch(InterruptedException e2) {
      System.out.print("This didnt work - exception 2");
    }

    System.out.println("Done");
  }
} 

Upvotes: 1

Views: 423

Answers (2)

Jarek Przygódzki
Jarek Przygódzki

Reputation: 4402

If you need advanced splitting you can use com.google.common.base.Splitter from Google Guava

Upvotes: 0

MeBigFatGuy
MeBigFatGuy

Reputation: 28568

String[] parts = line.split("\\s+");

probably

Upvotes: 2

Related Questions