Reading contents of a file into a 2D array

2020-03-31 08:08发布

问题:

I am fairly new to programming so layman's talk is appreciated.

I have been tasked to read the contents of a file, which will contain 9 values (3x3 array) and then place the contents in the corresponding index.

For instance,

The contents of the 2D array is...

1.00   -2.00   3.00   
4.00    1.00  -1.00   
1.00    2.00   1.00  

After the contents have been read in, they need to be printed. The program will then prompt the user for a scalar multiplier, such as '4.' The program should then print the new array with the new output.

I currently have this,

import java.io.*;
import java.util.*;


public class CS240Lab8a {

/**
 * @param args the command line arguments
 */
static double [][] matrix = new double[3][3];
static Scanner input = new Scanner(System.in);


public static void main(String[] args) throws IOException 
{
   // Variables
   String fileName = "ArrayValues.txt";




    // print description
   printDescription();

   // read the file
   readFile(fileName);

   // print the matrix
   printArray(fileName, matrix);

   // multiply the matrix
   multiplyArray(fileName, matrix);


}


// Program Description
        public static void printDescription()
        {
            System.out.println("Your program is to read data from a file named ArrayValues.txt and store the values in a 2D 3 X 3 array.  "
                    + "\nNext your program is to ask the user for a scalar multiplier \n"
                    + "which is then used to multiply each element of the 2D 3 X 3 array.\n\n"); 
        }



// Read File
        public static void readFile(String fileName) throws IOException
        {
            String line = "";

            FileInputStream inputStream = new FileInputStream(fileName);
            Scanner scanner = new Scanner(inputStream);
            DataInputStream in = new DataInputStream(inputStream);
            BufferedReader bf = new BufferedReader(new InputStreamReader(in));

            int lineCount = 0;
            String[] numbers;
            while ((line = bf.readLine()) != null)
            {
                numbers = line.split(" ");
                for (int i = 0; i < 3; i++)
                {
                matrix[lineCount][i] = Double.parseDouble(numbers[i]);
                }

                lineCount++;
            }
            bf.close();

        }


 // Print Array      
        public static void printArray(String fileName, double [][] array)
        {
            System.out.println("The matrix is:");

             for (int i = 0; i < 3; i++)
                {
                    for(int j = 0; j < 3; j++) 
                    {
                        System.out.print(matrix[i][j] + " ");
                     }
                    System.out.println();
                }
             System.out.println();
         }

        public static double multiplyArray(String fileName, double[][] matrix)
         {
                double multiply = 0;

                System.out.println("Please enter the scalar multiplier to be used.");
                multiply = input.nextDouble();

                for(int i = 0; i < 3; i++)
                {
                    for(int j = 0; j < 3; j++)
                    {
                        matrix[i][j] 

                return multiply;
          }



} // end of class

I currently have it printing the array properly.

What is the best way to multiply every index value by a constant (user input)?

回答1:

You're missing an extra step here.

Once you read the line, you have to then split the line and parseDouble on individual numbers.

int lineCount = 0;
while ((line = bf.readLine()) != null)
{
    String[] numbers = line.split(" ");
    for ( int i = 0 ; i < 3 ; i++) 
         matrix[lineCount][i] = Double.parseDouble(numbers[i]);

    lineCount++;
}

Also, your readFile doesn't need to return anything. Just make your matrix variable global.



回答2:

ok, the way I look to it: you read the content of the input file to a string. You already have the method for reading line by line just put everything in a string.

String content = readFile(input.txt);

// Parse lines

String[] lines = content.split("\n");

// Parses values

for(int i = 0; i < lines.length; i++)  {
    // Get line values
    String[] values = lines[i].split(" ");
    for(int j = 0; j < values.length; j++) {
        // Punt in Matrix
        matrix[i][j] = Double.parseDouble(values[j]);
    }
}


标签: java arrays io