2

I know that it is not a very exciting question, I think a basic JAVA user can solve it in a minute. Unfortunately I do not belong to those group of people (the code has been developed and tested by someone else). I am just trying to run this one JAVA code, that is an intermediate step in my data analysis (I use R and matlab for the rest of my analysis). When I run the JAR file I get the following message:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NumberFormatException: For input string: "-5.4240987837859231e+00    -1.6047620799956062e+00      6.5485351663678804e-01     -6.7434226427341926e-01"
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1241)
    at java.lang.Double.valueOf(Double.java:504)
    at app.Discovery.main(Discovery.java:35)

One of the input files is my data that is tab delimited txt file for my data. From what I can see it is not happy with the format of my output. I have tried to play around a bit with the format, but nothing worked:

Caused by: java.lang.NumberFormatException: For input string: "-5.4241 -1.6048  0.65485 -0.67434"
Caused by: java.lang.NumberFormatException: For input string: "-5.4240988e+00     -1.6047621e+00       6.5485352e-01      -6.7434226e-01"

I am not sure whether the format of the numbers is bothering Java or whether it is confused about the txt file being tab delimited. Anyone has any suggestions? All answers would be very much appreciated!

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
user3640711
  • 21
  • 1
  • 2

3 Answers3

5

This:

"-5.4241 -1.6048  0.65485 -0.67434"

is your problem. You should split your string on whitespace and submit each of the 4 numbers above to your formatter.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
0

Its taking the entire line as one number to parse in java number type (may be float or double ) i.e. "-5.4241 -1.6048 0.65485 -0.67434" which is invalid number. Your code should pick only either of value may be -5.4241 i.e. as per your functionality requirement.

Chatz
  • 1
0

You should use StringTokenizer to parse the string for the individual numbers. Use tab as the delimiter.

StringTokenizer vST = new StringTokenizer("-5.4241 -1.6048  0.65485 -0.67434", "\t"); // \t is for tab
while (vST.hasMoreTokens()) 
{
    System.out.println(Double.parseDouble(vST.nextToken()));
}
Naeem Iqbal
  • 385
  • 2
  • 5