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.

Community
  • 1
  • 1
Jonathan
  • 2,728
  • 10
  • 43
  • 73

2 Answers2

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()]);
}
Aubin
  • 14,617
  • 9
  • 61
  • 84
  • 1
    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] – CosmicGiant Nov 09 '12 at 17:57
  • @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 – Jonathan Nov 09 '12 at 19:10
  • 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 – Jonathan Nov 09 '12 at 19:20
  • Yes, my answer was Java 1.7, I'll edit it to compile in 6 – Aubin Nov 09 '12 at 19:28
  • @Aubin yes I had fixed that. But the program is still crashing. Now not because of the same error though – Jonathan Nov 09 '12 at 19:36
  • I have added some robustness tests, to avoid a crash with substring. – Aubin Nov 09 '12 at 19:39
  • [this](http://postimage.org/image/useals6yp/) is what I am getting. – Jonathan Nov 09 '12 at 19:46
  • Please post the code with line numbers to associate statck trace and code – Aubin Nov 09 '12 at 20:11
  • @Aubin I fixed it. sorry the error was from my side. not your code's logic. Thank you so much for your help :) – Jonathan Nov 09 '12 at 20:27
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
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133