tags:

views:

423

answers:

6

Hi, I'm a begginer in java I have

packet=090209153038020734.0090209153039020734.0

like this I want to split this string and store into an array like two strings:

1) 090209153038020734.0
2) 090209153039020734.0

I have done like this:

String packetArray[] = packets.split(packets,Constants.SF);

Where: Constants.SF=0x01.

But it won't work.

Please help me.

A: 

You don't say how you want to do the split. It could be based on a fixed length (number of characters) or you want one decimal place.

If the former you could do packetArray = new String[]{packet.substring(0, 20), packet.substring(21)};

int dotIndex = packets.indexOf('.');
packetArray = new String[]{packet.substring(0, dotIndex+2), packet.substring(dotIndex+2)};

Your solution confuses the regexp with the string.

Nick Fortescue
A: 

split uses regular expressions as documented here. Your code seems to be trying to match the whole string Constants.SF = 0x01 times, which doesn't make much sense. If you know what char the boxes are then you can use something like {[^c]+cc} where c is the character of the box (i guess this is 0x01), to match each "packet".

I think you are trying to use it like the .net String.Split(...) function?

jheriko
+2  A: 

The JavaDoc of String is helpful here: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

You have your String packet;

String.indexOf(String) gives you a position of a special substring. your interested in the "." sign. So you write

int position = packet.indexOf(".")+1

+1 becuase you want the trailing decimal too. It will return something 20-ish and will be the last pos of the first number.

Then we use substring

String first = packet.substring(0,position) will give you everything up to the ".0" String second = packet.substring(position-1) should give you everything starting after the ".0" and up to the end of the string.

Now if you want them explicitely into an array you can just put them there. The code as a whole - I may have some "off by one" -bugs.

int position = packet.indexOf(".")+1 
String first = packet.substring(0,position)
String second = packet.substring(position-1)
String[] packetArray = new String[2];
packetArray[0] = first;
packetArray[1] = second;
Jens Jansson
+3  A: 

I'd think twice about using split since those are obviously fixed width fields.

I've seen them before on another question here (several in fact so I'm guessing this may be homework (or a popular data collection device :-)) and it's plain that the protocol is:

  • STX (0x01).
  • 0x0f.
  • date (YYMMDD or DDMMYY).
  • time (HHMMSS).
  • 0x02.
  • value (XXXXXX.X).
  • 0x03.
  • 0x04.

And, given that they're fixed width, you should probably just use substrings to get the information out.

paxdiablo
+1  A: 
String packetArray[] = packets.split("\u0001");

should work. You are using

public String[] split(String regex, int limit)

which is doing something else: It makes sure that split() returns an array with at most limit members (1 in this case, so you get what you ask for).

Aaron Digulla
+1  A: 

You need to read the Javadocs for the String.split() methods...you are calling the version of String.split() that takes a regular expression and a limit, but you are passing the string itself as the first parameter, which doesn't really make sense.

As Aaron Digulla mentioned, use the other version.

Jeff Olson