读取文件的内容到2D阵列 [英] Reading contents of a file into a 2D array

查看:126
本文介绍了读取文件的内容到2D阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的节目让外行人的谈话是AP preciated。

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

我一直负责读取文件,其中将包含9个值(3×3),然后将内容在相应的索引中的内容。

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.

例如,

二维数组的内容是...

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  

的内容已被读出后,他们需要被打印。然后程序会提示用户输入一个标量倍增,如4然后程序应该打印与新的输出新的数组。

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.

目前,我有这个,

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)?

推荐答案

您在这儿缺少一个额外的步骤。

You're missing an extra step here.

在你读行,你必须再拆对个别号线与parseDouble。

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

此外,您READFILE不需要任何回报。只要你的矩阵变量全局性的。

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

这篇关于读取文件的内容到2D阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆