2

Possible Duplicate:
Split string to equal length substrings in Java

I have a String[] data-structure, which is like this

0.1953601.3675211.3675214.1025640.5860811.36752110.3540903.711844-5.2747252.539683

I want to split this into an array like this

  0.195360
  1.367521
  1.367521
  4.102564
  0.586081
  1.367521
 10.354090
  3.711844
 -5.274725
  2.539683

so after the decimal the values have 6 significant figures

I tried using regex solution from this question but it doesnt seem to work.

even this

System.out.println(Arrays.toString(
  "Thequickbrownfoxjumps".split("(?<=\\G.{4})")
));

gives me an output of [Theq, uickbrownfoxjumps] not [Theq, uick, brow, nfox, jump, s] how I would expect it to.

4
  • @SeanPatrickFloyd yes, I addressed that in the question itself. that solution provided there doesn't seem to work Commented Nov 9, 2012 at 17:17
  • Yes it does: ideone.com/slREBM Commented Nov 9, 2012 at 17:17
  • Or use my Guava-Answer: stackoverflow.com/a/3760862/342852 Commented Nov 9, 2012 at 17:18
  • Please take in account my proposal to correct your assertion about the items length Commented Nov 9, 2012 at 17:45

2 Answers 2

4

The assertion

the size of each value is 8, but if the value is negative its 9

in the question is false because if I split the entry manually here is the result:

0.195360
1.367521
1.367521
4.102564
0.586081
1.367521
10.35409
03.71184   <<< As you can see, it's not that you want
4-5.2747   <<< It's not a number
252.5396
83         <<< Bang! too short

I presume the true assertion is "the number of digit after dot is 6", in this case the split becomes:

  0.195360
  1.367521
  1.367521
  4.102564
  0.586081
  1.367521
 10.354090
  3.711844
 -5.274725
  2.539683

The code is here:

static String[] split( String in ) {
   List< String > list = new LinkedList< String >();
   int dot = 0;
   for( int i = 0; dot > -1 && i < in.length(); i = dot + 7 ) {
      dot = in.indexOf( '.', i );
      if( dot > -1 ) {
         int last = Math.min( dot + 7, in.length());
         list.add( in.substring( i, last ));
      }
   }
   return list.toArray( new String[list.size()]);
}
Sign up to request clarification or add additional context in comments.

9 Comments

It's possible the assumption is incorrect, as 1.367521->10.354090 might be 1.3675211->0.354090, for example. But I also believe guesstimating by the number digits after the dot to be the most reasonable logic. [+1]
@Aubin your assumption might be right. Its eeg data so it might be how you are describing it. I just got the text file, the original data I'll have to go to the lab and check. But regarding the code, I am getting an error for linkedlist its saying '<>' operator is not allowed for source level below 1.7
and since I am working in android if I change the level it says Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead
Yes, my answer was Java 1.7, I'll edit it to compile in 6
@Aubin yes I had fixed that. But the program is still crashing. Now not because of the same error though
|
0

Not a great application of regular expression IMHO:

 while not end-of-string
   if next-char is "-"
      take next 9 chars
   else
      take next 8 chars

1 Comment

It doesn't work since the spec is erroneous

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.