将文件内容读入2D数组

时间:2011-11-08 02:25:24

标签: java arrays io

我对编程很新,所以非常感谢外行。

我的任务是读取一个文件的内容,该文件将包含9个值(3x3数组),然后将内容放在相应的索引中。

例如,

2D数组的内容是......

1.00   -2.00   3.00   
4.00    1.00  -1.00   
1.00    2.00   1.00  

读完内容后,需要打印。然后程序将提示用户输入标量乘数,例如'4'。然后程序应该用新输出打印新数组。

我目前有这个,

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

我目前正在正确打印数组。

将每个索引值乘以常量(用户输入)的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

你在这里错过了一个额外的步骤。

读完该行后,您必须将行和parseDouble拆分为各个数字。

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++;
}

此外,您的readFile不需要返回任何内容。只需将矩阵变量设为全局变量即可。

答案 1 :(得分:0)

好的,我看待它的方式: 您将输入文件的内容读取为字符串。您已经有了逐行阅读的方法,只需将所有内容放入字符串中即可。

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]);
    }
}