Results 1 to 18 of 18
- 04-07-2011, 02:32 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Reading Text file to Data type Double
Hi All,
I am currently working on a file which will read a text file which contains data for an array, and then uses that data to create a plot.
My issue is that while I am able to read the text file and store the data as a string, I am finding it difficult to convert this string to a Double [][] array to be used in the plotting.
Here's the section of code I'm using to hopefully accomplish this:
try{
File textfile1 = new File("/Users/malkaysi/Downloads/test2.txt");
FileReader fileReader = new FileReader(textfile1);
BufferedReader reader = new BufferedReader(fileReader);
String line = null;
while((line = reader.readLine()) !=null) {
// System.out.println(line);
// This is where I am trying to convert the string to the double array, however there is no output..
double data = Double.parseDouble(line);
System.out.println(data);
}
reader.close();
} catch (Exception ex) {
}
Thanks!
- 04-07-2011, 02:35 AM #2
And?
Do you get errors? Do your get unexpected behaviour? Do your get incorrect output?
- 04-07-2011, 02:37 AM #3
Ahh. no output. If that is true then the while loop is never entered. Which means the very first read returns null. Make sure you are reading the correct file. Make sure the file acutally has data.
- 04-07-2011, 02:38 AM #4
NEVER NEVER NEVER do this. Your code is throwing an exception but you have no idea what it is.Java Code:} catch (Exception ex) { }
- 04-07-2011, 04:49 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
When I run it, I get no output.
I know it has the correct file as when I comment out the:
double data = Double.parseDouble(line);
System.out.println(data);
and instead run
System.out.println(line)
I see the correct array as output. Somewhere in there, it is losing the string...
- 04-07-2011, 04:52 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Here's the exception I am getting when I attempt to run the file (changing the exception ex to IOException e)
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at TG1.jButton4ActionPerformed(TG1.java:413)
at TG1.access$300(TG1.java:23)
at TG1$4.actionPerformed(TG1.java:119)
at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.jav a:6374)
at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
at java.awt.Component.processEvent(Component.java:613 9)
at java.awt.Container.processEvent(Container.java:208 5)
at java.awt.Component.dispatchEventImpl(Component.jav a:4736)
at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
at java.awt.Component.dispatchEvent(Component.java:45 66)
at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478 )
at java.awt.Component.dispatchEvent(Component.java:45 66)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:680)
at java.awt.EventQueue.access$000(EventQueue.java:86)
at java.awt.EventQueue$1.run(EventQueue.java:639)
at java.awt.EventQueue$1.run(EventQueue.java:637)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:653)
at java.awt.EventQueue$2.run(EventQueue.java:651)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java: 650)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
- 04-07-2011, 04:57 AM #7
"{{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,1,1 ,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},"
Obviously that is not a valid double value. So instead of trying to convert the entire line to a double, break it down and convert all the individual numbers.
- 04-07-2011, 05:06 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Ahh can I do this for multiple lines? The text file actually contains a 25 x 40 array.
- 04-07-2011, 05:09 AM #9
You are going to need nested loops. The outer loop does 25 rows. The inner loop does 40 columns.
- 04-07-2011, 05:10 AM #10
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
Great! Let me give this a try and I'll report back!
- 04-07-2011, 05:11 AM #11
BTW, do not hard code your loops to iterate 25 and 40 times.
- 04-07-2011, 04:06 PM #12
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
import java.io.*;
class testingreadingfile {
public static void main (String [] args){
try{
File myfile = new File("/Users/malkaysi/Downloads/test4.txt");
FileReader fileReader = new FileReader (myfile);
BufferedReader reader = new BufferedReader (fileReader);
String line = null;
while((line = reader.readLine()) !=null) {
// System.out.println(line);
Double data [][] = new Double[4][10];
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 10; j++)
{
data[i][j] = Double.parseDouble(line);
}
System.out.println(data);
}
reader.close();
} }
catch (IOException e) {
}
}
}
Here's my updated code....I'm still getting a numberformatexception....
Exception in thread "main" java.lang.NumberFormatException: For input string: "{\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf54 0"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at testingreadingfile.main(testingreadingfile.java:27 )
Java Result: 1
the text file simply contains:
1 2 3 4 5 6 7 8 9 0
2 3 2 2 1 6 5 4 3 2
2 3 4 2 1 6 7 4 3 8
7 8 4 3 2 6 7 8 9 2
also, I know I hardcoded the array size. I figure I will go back and correct this once I get it to convert properly.
- 04-07-2011, 11:33 PM #13
You are still trying to parse the entire line into a double. You need to break the line down and parse each individual value.Java Code:data[i][j] = Double.parseDouble(line);
You have 3 loops but you only need 2. The outer loop will do the rows(lines in the file). An inner loop to do values in a single line.
Also you are creating a new 2D array each time around the while loop.
- 04-08-2011, 02:25 AM #14
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
okay so I've cleaned it up a bit...
import java.io.*;
class testingreadingfile {
public static void main (String [] args){
try{
File myfile = new File("/Users/malkaysi/Downloads/test3.txt");
FileReader fileReader = new FileReader (myfile);
BufferedReader reader = new BufferedReader (fileReader);
String line = null;
double[] data = new double[line.length()];
while((line = reader.readLine()) !=null) {
//System.out.println(line);
for (int i = 0; i < line.length(); i++) {
// String[] result = line.split(",");
data[i] = Integer.parseInt(line);
System.out.println(data);
}
reader.close();
}}
catch (IOException e) {
}
}
}
Currently I am getting a NullPointException in line 18 (double[] data = new double[line.length()];).
I am aware this is due to the fact that I am referring to a statement with a null, but is there a way to surpass this?
Thanks for all the help thus far!
-
Yeah, don't try to call a method on a variable that you know doesn't refer to an object. That line simply makes no sense at all. What is the length of something that doesn't exist?
-
why not just get rid of that line? And why the for loop that loops on line length which is somewhat meaningless rather than data length which means a lot more?
- 04-08-2011, 03:07 AM #17
Member
- Join Date
- Apr 2011
- Posts
- 8
- Rep Power
- 0
I thought that the reader would loop over the rows in the file, while the for loop would loop the length of each row (ie the number of columns). Is this not what I am doing?
As for the troublesome line, how should I define the double array in another way?
Cheers!
-
The length of a String is simply the char count of the String. It has absolutely nothing to do with number of rows of anything.
You create the array object in the split method and use it only after you've created it.As for the troublesome line, how should I define the double array in another way?
You'd best think through your program logic on paper a bit.
Similar Threads
-
Read a data from a text file and create an object from these data in this text file
By jjavaa in forum Advanced JavaReplies: 2Last Post: 03-25-2011, 02:36 PM -
read only double input from text file
By napi1234 in forum New To JavaReplies: 6Last Post: 06-28-2010, 04:06 PM -
Problem Reading in data from Text Field
By markious in forum AWT / SwingReplies: 2Last Post: 02-12-2010, 02:29 AM -
Reading data from a text file
By Cheguvara in forum New To JavaReplies: 2Last Post: 02-02-2010, 02:33 PM -
reading data from text file .. help plz
By Thug heart in forum New To JavaReplies: 7Last Post: 02-15-2009, 07:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks