4

Part of my application I encountered this problem. The String line variable contains 12.2 Andrew and I'm trying to split them separately but it doesn't work and comes with a NumberFormatException error. Could you guys help me on that please?

String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");

System.out.println(Double.valueOf.(data[0]));
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Scorpiorian83
  • 469
  • 1
  • 4
  • 17

3 Answers3

5

Did you look at your data variable? It didn't split anything at all, since the condition never matches. You are looking for a place in the input immediately after a number and before a letter, and since there is a space in between this doesn't exist.

Try adding a space in the middle, that should fix it:

String[] data = line.split("(?<=\\d) (?=[a-zA-Z])");
Keppil
  • 45,603
  • 8
  • 97
  • 119
  • oh thanks so much I just learnt about these regular expressions and just copied them somewhere from my research. Didn't know the space was important as well. Cheers! – Scorpiorian83 Aug 27 '14 at 13:43
  • Do you think you can have a look at my previous question relating to this issue? Thanks again mate! http://stackoverflow.com/questions/25485260/how-do-i-achieve-the-following-results-using-the-printerwriter-class-from-a-text – Scorpiorian83 Aug 27 '14 at 14:08
2

If you print content of data[0] you will notice that it still contains 12.2 Andrew so you actually didn't split anything. That is because your regex says:
split on place which has digit before and letter after it

which for data like

123foo345bar 123 baz

effectively can only split in places marked with |

123|foo345|bar 123 baz
                  ^it will not split `123 baz` like
                   `123| baz` because after digit is space (not letter)
                   `123 |baz` before letter is space (not digit)
                   so regex can't match it

What you need is to "split on space which has digit before and letter after it" so use

String[] data = line.split("(?<=\\d)\\s+(?=[a-zA-Z])");
//                                  ^^^^ - this represent one ore more whitespaces
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • hi there thanks for your information here can I ask if you could also help me on a previous question relating to this issue? Cheers! http://stackoverflow.com/questions/25485260/how-do-i-achieve-the-following-results-using-the-printerwriter-class-from-a-text – Scorpiorian83 Aug 27 '14 at 14:08
  • @Scorpiorian83 Answers from question you linked seems valid. What problem you have with them? – Pshemo Aug 27 '14 at 14:23
  • i'm using the solution from Mena at the moment it works right until the third line as it begins with a string and then double and it gives an exception from there. – Scorpiorian83 Aug 27 '14 at 14:49
  • @Scorpiorian83 And [this answer](http://stackoverflow.com/a/25486313/1393766) shows how you should handle this situation. – Pshemo Aug 27 '14 at 14:52
2

Your split is not working, and not splitting the String. Therefore Double.parseDouble is parsing the whole input.

Try the following:

String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
// System.out.println(Double.valueOf(data[0]));
// fixed
data = line.split("(?<=\\d).(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
System.out.println(Double.valueOf(data[0]));

Output

[12.2 Andrew]
[12.2, Andrew]
12.2
Mena
  • 47,782
  • 11
  • 87
  • 106
  • oh hey hi there thanks for helping me out here. Do you think you can have a look at my previous question relating to this issue? I'm very stuck on it. Cheers. http://stackoverflow.com/questions/25485260/how-do-i-achieve-the-following-results-using-the-printerwriter-class-from-a-text – Scorpiorian83 Aug 27 '14 at 14:07
  • Using dot is not best choice. This way you will split also `123foo` on `f` which will give you as result `[123, oo]`. – Pshemo Aug 27 '14 at 15:12
  • @Pshemo I'd agree with you if there were any more cases, but it does work just as well with the provided input. – Mena Aug 27 '14 at 15:35