Results 1 to 18 of 18
  1. #1
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default 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!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    And?

    Do you get errors? Do your get unexpected behaviour? Do your get incorrect output?

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    } catch (Exception ex) {
    }
    NEVER NEVER NEVER do this. Your code is throwing an exception but you have no idea what it is.

  5. #5
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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...

  6. #6
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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)

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    "{{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.

  8. #8
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Ahh can I do this for multiple lines? The text file actually contains a 25 x 40 array.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You are going to need nested loops. The outer loop does 25 rows. The inner loop does 40 columns.

  10. #10
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    Great! Let me give this a try and I'll report back!

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    BTW, do not hard code your loops to iterate 25 and 40 times.

  12. #12
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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.

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Java Code:
    data[i][j] = Double.parseDouble(line);
    You are still trying to parse the entire line into a double. You need to break the line down and parse each individual value.

    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.

  14. #14
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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!

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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?

  17. #17
    orpheus2708 is offline Member
    Join Date
    Apr 2011
    Posts
    8
    Rep Power
    0

    Default

    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!

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by orpheus2708 View Post
    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?
    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.

    As for the troublesome line, how should I define the double array in another way?
    You create the array object in the split method and use it only after you've created it.

    You'd best think through your program logic on paper a bit.

Similar Threads

  1. Replies: 2
    Last Post: 03-25-2011, 02:36 PM
  2. read only double input from text file
    By napi1234 in forum New To Java
    Replies: 6
    Last Post: 06-28-2010, 04:06 PM
  3. Problem Reading in data from Text Field
    By markious in forum AWT / Swing
    Replies: 2
    Last Post: 02-12-2010, 02:29 AM
  4. Reading data from a text file
    By Cheguvara in forum New To Java
    Replies: 2
    Last Post: 02-02-2010, 02:33 PM
  5. reading data from text file .. help plz
    By Thug heart in forum New To Java
    Replies: 7
    Last Post: 02-15-2009, 07:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •