-2

I'm having trouble trying to write the syntax in Java to do what I want to do with this file

What i'm doing is reading in a file and I want to add specific lines of the file to elements of a 2D array, I have written some pseudo code below to help understand what im trying to do

    try
    {
        FileInputStream fStream = new FileInputStream(fileName);
        DataInputStream inStream = new DataInputStream(fStream);
        BufferedReader brRead = new BufferedReader (new InputStreamReader(inStream));

        String line;

        while ((line = brRead.readLine()) !=null)
        {
            //Lines 1-3 
            //{
            // Add line 1 into element 0,0 of my array
            // Add line 2 into element 0,1 of my array
            // Add line 3 into element 0,2 of my array
            //}

            //Lines 4-10
            //{
            // Add line 4 into element 1,0 of my array
            // Add line 5 into element 1,1 of my array
            // ect..
            //}
        }
    }
    catch (Exception e)
    {

    }
}
Joel
  • 9
  • 2

1 Answers1

0

Suppose the 2d arrays are all 3 x 3 (not explained clearly.) The algorithm and code is probably similar to below, which is quite simple.

try
{
    FileInputStream fStream = new FileInputStream(fileName);
    DataInputStream inStream = new DataInputStream(fStream);
    BufferedReader brRead = new BufferedReader (new InputStreamReader(inStream));

    String line;
    int lineCount = 0;
    int mod9 = -1;
    int firstIndex = -1;
    int secondIndex = -1;
    double[][] array = new double[3][3];
    while ((line = brRead.readLine()) !=null)
    {
        //Lines 1-3 
        //{
        // Add line 1 into element 0,0 of my array
        // Add line 2 into element 0,1 of my array
        // Add line 3 into element 0,2 of my array
        //}
        mod9 = lineCount % 9 ;
        firstIndex = mod9 / 3;
        secondIndex = mod9 % 3;
        array[firstIndex] += Double.parseDouble(line);
        lineCount++;
        //Lines 4-10
        //{
        // Add line 4 into element 1,0 of my array
        // Add line 5 into element 1,1 of my array
        // ect..
        //}
    }
}
catch (Exception e)
{

}
Tom
  • 3,168
  • 5
  • 27
  • 36