• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

taking part of the string

Hi,
I have an output of the "adb devices" command, it's working and showing the output,
what I need is to only print the device name..

here is the code:
public void newExec() throws IOException, InterruptedException, BadLocationException{
		String adbPath = "/Volumes/development/android-sdk-macosx/tools/adb";
		String cmd = adbPath+" "+"devices";
		
		Process p;
		p=Runtime.getRuntime().exec(cmd);
		p.waitFor();
		
		String line;
		
		BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
		while ((line = err.readLine()) != null){
			System.out.println(line);
		}
		err.close();
		
		BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
		while((line=input.readLine())!=null){
			//printing in the console 
			System.out.println(line);

//copy to the text area
			textArea.append("\n"+line);
			
		}
		input.close();
}

Open in new window


and here is the output:
List of devices attached

192.168.56.101:5555      device

What I want is to print only the device name which is "192.168.56.101:5555"
I tried using split before this line"System.out.println(line);"  but I could not be able to do that..

any help :)

thanks :)
0
omarkkk
Asked:
omarkkk
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
dpearsonCommented:
What went wrong with split?

This should work:

            String line = "192.168.56.101:5555      device" ;
            String[] words = line.split(" ") ;
            System.out.println("First word " + words[0]) ;

Doug
0
 
omarkkkAuthor Commented:
Hi Doug,
The problem is that the device name is not static
sometimes it's this "192.168.56.101:5555" but some times when using another device "I89613ba062a",
what i need is to split the device name even if i change it!
and only device name without the word device, so if possible only this part "192.168.56.101:5555"
so it's like splitting from the second line, and only taking devices name without device word!
thanks :)
0
 
CEHJCommented:
The split is not based on the name, it's based on the delimiter
Please show what the raw output is without the splitting
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
dpearsonCommented:
Yeah like CEHJ said, if you split on " " it will give you everything before and after a space.

So:

"I89613ba062a device"
"12312312.I89613ba062a                                    device"

should work fine.

However:
"I89613ba062a_________device"
"I89613ba062a.device"

wouldn't work (no spaces)

Another way to solve this is find where the word "device" appears in the line and return everything up to that:

             String line = "192.168.56.101:5555      device" ;
            int devicePos = line.indexOf("device") ;
            if (devicePos == -1)
                  devicePos = line.length() ;
            String rest = line.substring(0, devicePos).trim() ;
            System.out.println("All except device: " + rest) ;

Doug
0
 
awking00Commented:
This works with or without spaces
System.out.println(line.replaceAll("device","").trim());
0
 
omarkkkAuthor Commented:
I've requested that this question be deleted for the following reason:

not good question!
0
 
CEHJCommented:
omarkkk, please select the comments that have been given. People here have tried to help you
0
 
awking00Commented:
omarkkk,
Did you try my suggestion? If you did and it didn't work, can you provide an example where it failed?
0
 
omarkkkAuthor Commented:
I've requested that this question be deleted for the following reason:

none
0
 
awking00Commented:
No reason you shouldn't!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now