NumberFormatException

When i'm trying to integrate 2 of my programs I was stuck wid this exception

Exception in thread"AWT-EventQueue-0" java.lang.NumberFormatException: empty String

at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)

at java.lang.Double.parseDouble(Double.java:482)

at DataAnalysis.readfile(DataAnalysis.java:351)

at DataAnalysis$submit.actionPerformed(DataAnalysis.java:218)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1849)

at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2169)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:234)

at java.awt.Component.processMouseEvent(Component.java:5488)

at javax.swing.JComponent.processMouseEvent(JComponent.java:3126)

at java.awt.Component.processEvent(Component.java:5253)

at java.awt.Container.processEvent(Container.java:1966)

at java.awt.Component.dispatchEventImpl(Component.java:3955)

at java.awt.Container.dispatchEventImpl(Container.java:2024)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4212)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3892)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3822)

at java.awt.Container.dispatchEventImpl(Container.java:2010)

at java.awt.Window.dispatchEventImpl(Window.java:1774)

at java.awt.Component.dispatchEvent(Component.java:3803)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:463)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:242)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:163)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:157)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:149)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:110)

I'm trying to integrate 2 classes namely DataPrep class and DataAnalysis class. I couldn't able to understand where exactly the exception been raised and what exactly I need to do.

[2444 byte] By [sarath44a] at [2007-11-27 4:55:58]
# 1

Look at the 4th line down in your error message:

at DataAnalysis.readfile(DataAnalysis.java:351)

Go to the DataAnalysis class, line 351, and you'll see where your code is tripping up.

At that spot, your program is trying to convert a string to a number but the string is not in the proper format (for instance not a number, or the string represents a double but you're trying to convert it to int...).

petes1234a at 2007-7-12 10:11:02 > top of Java-index,Java Essentials,Java Programming...
# 2

Below is the method which reads the file.

public void readfile(File retfile)

{

StringBuffer sb=new StringBuffer();

//int rows=0;

int totalvalues=0;

//int columns=0;

String s[];

try

{

//scanner is used to scan the textfile

Scanner sc=new Scanner(retfile);

//scan each line in the text file

while(sc.hasNextLine())

{

sb.append(sc.nextLine());

rows++;

}

}

catch(Exception e)

{

System.out.println("Exception"+e);

}

s=(sb.toString()).split("");

totalvalues=s.length;

columns=totalvalues/rows;

System.out.println("rows:"+rows+"Columns"+columns);

double normaldata[][]=new double[rows+1][columns+1];

int k=0;

for(int i=1;i<=rows;i++)

{

for(int j=1;j<=columns;j++)

{

normaldata[i][j]=Double.parseDouble(s[k]);

k++;

}

}

for(int i=1;i<=rows;i++)

{

for(int j=1;j<=columns;j++)

{

System.out.print(normaldata[i][j]+"");

}

System.out.println();

}

That is the code that is present from line 309 to line 363

The file contains the data of type double. The data that is in the file is given below

0.330.000.00

0.500.000.00

0.670.000.00

0.750.000.00

0.420.000.00

0.080.130.00

0.750.000.00

0.420.000.00

0.920.000.00

0.500.000.00

0.080.130.00

0.580.000.00

0.580.000.00

1.000.000.00

0.000.630.00

0.000.500.00

0.080.130.00

0.330.000.00

0.000.500.00

0.330.000.00

0.080.130.00

0.330.000.00

0.750.000.00

0.330.000.00

0.580.000.00

0.420.000.00

0.420.000.00

0.250.000.00

0.250.000.00

0.670.000.00

So could you please tell me where I've went wrong?

Message was edited by:

sarath44

sarath44a at 2007-7-12 10:11:02 > top of Java-index,Java Essentials,Java Programming...
# 3

The error message and code you've posted don't exactly agree, but I'm guessing the error is onnormaldata[i][j]=Double.parseDouble(s[k])

and the message is telling you what the problem is: s[k] is an empty string.

So just before the start of the loop print out the length and contents of the s array - so you can check it is as you expect. If it is not, check the API documentation for the split() method you use to populate the array.

pbrockway2a at 2007-7-12 10:11:02 > top of Java-index,Java Essentials,Java Programming...
# 4

Yup. Your problem is with line:

normaldata[i][j]=Double.parseDouble(s[k]);

At the time of the exception, s[k] does not represent a double number. Why don't you put that portion of the code in a try / catch loop, and in the catch, output the s[k] with System.out.println("k = " + k + ", and s[k] = " + s[k]);

Let me know what turns up.

Good luck!

petes1234a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 5
dang, I type too slow!
petes1234a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 6

When I'm trying to get the length of s i'm getting its length as 599 instead of 600. And the exception that it is showing is

S length is:599

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String

at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)

at java.lang.Double.parseDouble(Double.java:482)

at DataAnalysis.readfile(DataAnalysis.java:349)

at DataAnalysis$submit.actionPerformed(DataAnalysis.java:215)

So could you tell me where I went wrong

sarath44a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 7

When i kept it in try catch block this is what i was getting

S length is:599

Exception internally:java.lang.NumberFormatException: empty String

It could only be able to read 0.33 and at the rest it is reading 0.00 in place of the data.

This is where i included my try catch block

double normaldata[][]=new double[rows+1][columns+1];

int k=0;

System.out.println("S length is:"+s.length);

try {

for(int i=1;i<=rows;i++)

{

for(int j=1;j<=columns;j++)

{

normaldata[i][j]=Double.parseDouble(s[k]);

k++;

}

}

}

catch(Exception e)

{

System.out.println("Exception internally:"+e);

}

sarath44a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 8
> S length is:599How many lines are there in your data file? 200, or more?
pbrockway2a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 9
there are in totatl 150 rows ..and each row has 3 columns
sarath44a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 10

> there are in totatl 150 rows ..and each row has 3 columns

So you are looking for 450 numbers, not 600 (or 599).

And something is going very wrong where you read each of these 150 rows. Try just reading a few rows, then print out the contents of both the stringbuffer (sb) and the array (s).

pbrockway2a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 11

I'll bet that you will need to change this:

double normaldata[][]=new double[rows+1][columns+1];

int k=0;

for(int i=1;i<=rows;i++)

{

for(int j=1;j<=columns;j++)

{

normaldata[i][j]=Double.parseDouble(s[k]);

k++;

}

}

to this:

double normaldata[][]=new double[rows][columns];

int k=0;

for(int i = 1; i < rows; i++)

{

for(int j = 1; j < columns; j++)

{

normaldata[i][j] = Double.parseDouble(s[k]);

k++;

}

}

Try it and see what happens.

Or maybe not. I didn't see the 150 row business til after this post.

Message was edited by:

petes1234

petes1234a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 12
Nothing much been changed.Am getting the same exception.It could just be able to accept 0.33 an there after it is takin 0.00 for the remaining data
sarath44a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 13
For the third and last time before I'm out of here: what happens when you read a few lines and then examine (by printing them) the contents of the string buffer and the array?@anybody else who might persue this: beware the white space in reply 2.
pbrockway2a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 14
> @anybody else who might persue this: beware the white> space in reply 2.?
petes1234a at 2007-7-12 10:11:03 > top of Java-index,Java Essentials,Java Programming...
# 15
> ?s=(sb.toString()).split("<tab>");0.33<tab>0.00<tab>0.00<tab><tab>0.50<tab>0.00<tab>0.00<tab><tab>etc
pbrockway2a at 2007-7-21 21:16:15 > top of Java-index,Java Essentials,Java Programming...