-3

I have written a split function whose constructor takes a String Object (For Example: 21.316621 87.01393 9830.686907).Then it splits it into three parts using split() function. Then converts these three new String Object into three primitive type double. I want to perform some arithmetic operation on these double values but it is showing Exception as following--

Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.lang.Double.parseDouble(Double.java:538)

Split function is as following--

public class Split {


public Split(String str){


Double[] lla = new Double[3];


    String[] parts=str.split("\t");


lla[0] = Double.parseDouble(parts[0]);


lla[1] = Double.parseDouble(parts[1]);
 lla[2]= Double.parseDouble(parts[2]);
 lla[0]+=123.567;
 lla[1]+=12.456;
 lla[2]+=76.5678;
 System.out.println(lla[0]);
 System.out.println(lla[1]);
 System.out.println(lla[2]);
 System.out.println(); 
 }  
 }

Please help me out of this problem.Thanks

  • What do you expect to happen if one uses two or more tabs to delimit each number? (this is your problem here) – Tom Jul 03 '16 at 13:22
  • 1
    Use your debugger. Add System.out.println() in the code to find the problem. The message is pretty clear: one of your parts is in fact an empty string. This is debugging 101. – JB Nizet Jul 03 '16 at 13:23
  • are you sure that you pass true string to your function? and also what about "\t" are you sure that your strings are splited by "\t" and not spaces? – pouyan Jul 03 '16 at 13:23
  • @pooyan *"are you sure that your strings are splited by "\t" and not spaces?"* the exception message would be different. – Tom Jul 03 '16 at 13:24
  • @Tom you are right but if for example his string split with space instead of "\t" it causes that there were wrong char in his numbers that will cause NumberFormatException when he try to convert it to double.ex: "2.19 2.2\t 3.4" ===> ["2.19 2.2"], ["3.4"]. – pouyan Jul 03 '16 at 13:31
  • @pooyan His exception *"NumberFormatException: empty String"* and not *"NumberFormatException: 2.19 2.2"*. So your concern about the used delimiter is usually ok, escpecially if one tries to split with tab, but the issue here is a different one. – Tom Jul 03 '16 at 13:34
  • 1
    @Tom again you are right but i just give you an example to understand my reason. an example that i can gave you for "NumberFormatException: empty String" is this: "2.19 2.2\t 3.4\t " ===>["2.19 2.2"],["3.4"],[" "] – pouyan Jul 03 '16 at 13:36
  • @pooyan I guess you meant `"2.19\t2.2\t3.4\t "`, because OPs code won't reach the empty String, if the parts before it are invalid. But yes, this is also a possible case. – Tom Jul 03 '16 at 13:40

2 Answers2

-1

This error means that one of these : parts[0], parts[1], parts[2] is an empty String and doesn't contain a double value. So Java coudn't parse it to a Double. Check your input String that you're splitting to create the parts array.

If you can't modify your input, then check and remove the empty values from the parts array before parsing them.

Check a working and not working example (throwing the same error) for the same method but different inputs here.

Ashish Ranjan
  • 5,523
  • 2
  • 18
  • 39
-1

If you're using , replace

String[] parts = str.split("\t")

with the following

String[] parts = Stream.of(str.split("\\s"))       // Split at each whitespace
                       .filter(x -> !x.isEmpty())  // Exclude empty Strings
                       .toArray(String[]::new);    // Makes a new String array of it

To make sure not empty String makes it to the parse section of the constructor.

Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
  • Now the sad part: I totally missed additional tab characters at the beginning or the end of `str`, which isn't currently handled, but were in your first approach ^^. Sorry. Btw: I haven't voted here. – Tom Jul 03 '16 at 13:41
  • Me neither, but I guess it happend due to the incorrect handling of tabs at the beginning or the end of the String? (which is my fault though) – Tom Jul 03 '16 at 13:48
  • @Tom Well I don't know what to think, two downvotes on a working code... – Yassin Hajaj Jul 03 '16 at 13:52