Results 1 to 4 of 4
  1. #1
    dilley is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default fill a rectangular array from a text file of doubles

    Hello,
    I am new to java and working on a project which will calculate the Hydraulic cylinder needed to hold a specific component in place while molding. I am trying to importalues into an array so i can work with the values in my application. The values are in a text file called "Cylinder Pressure Chart.txt" and is layed out as sown below.

    10.5,290,30,45
    1.36,25,56,85

    This file currently contains 15 rows and 16 columns but this could change over time as new cylinder files are used. the code I have so far is shown below but I cant seem to get it even close to doing what I need it to do. Please help!! The array should be filled out like this


    double[][] numbers = new double[15][14];

    numbers [0][0] = 10.5
    numbers [0][1] = 290
    numbers [0][2] = 30
    numbers [1][0] = 1.36
    numbers [1][1] = 25
    and so on...

    Java Code:
            //Create Array
            double[][] numbers = new double[15][14];
            
            try 
            {
                br = new BufferedReader(new FileReader("Cylinder Pressure Chart.txt"));
                String line = null;
    
            while ((line = br.readLine()) != null) 
            {
                
                for(int i=0; i<=15; i++)
                {
                    for(int j=0; j<=14; j++)
                    {
                        numbers[i][j] = line.split("\t");
                    }
                }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: fill a rectangular array from a text file of doubles

    When you split a line you get an array of Strings (worth one row in your matrix?) You still have to convert those Strings to doubles.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    dilley is offline Member
    Join Date
    Jun 2012
    Posts
    2
    Rep Power
    0

    Default Re: fill a rectangular array from a text file of doubles

    Yes, The first line of data would be split and end up the first row of the array?Thats how I want it to happen anyway, Then read the next line and split it up into the parts for the second row and so on. I have been looking for help in other forums as well and found some examples but none seem to fit exactly what I am trying to do. Also java is very new for me..

  4. #4
    Ksharp is offline Banned
    Join Date
    Jun 2012
    Location
    Beijing,China
    Posts
    34
    Rep Power
    0

    Default Re: fill a rectangular array from a text file of doubles

    It is blaming time.

    [ please no spoonfeeding - mod ]

    Ksharp
    Last edited by JosAH; 06-18-2012 at 10:59 AM.

Similar Threads

  1. Replies: 15
    Last Post: 05-14-2012, 01:42 PM
  2. Can't fill the array with the object?
    By Clerek in forum New To Java
    Replies: 9
    Last Post: 02-21-2011, 01:31 AM
  3. Replies: 0
    Last Post: 12-30-2010, 10:12 AM
  4. [SOLVED] Java not fill in all info for text file
    By gotenks05 in forum New To Java
    Replies: 0
    Last Post: 05-24-2009, 06:17 PM
  5. Array Fill Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:45 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
  •