如何从.txt文件中读取数字并将其放入二维数组中?

时间:2019-01-31 17:20:06

标签: java

这是针对学校项目的。我需要从.txt文件中读取数字并将其放入数组中。将它们放入数组后,我需要将其传递到另一个类中以进行数学运算并比较数字。唯一的问题是,我无法获取读取.txt文件或将其放入数组的代码。

我需要使用

    if (sq.isMagicSquare())

将数组传递给Square类,但是会出现错误:

required: int[][]

found: no arguments

reason: actual and formal lists differ in length

public class MagicSquareTester
{
public static void main() throws IOException
{
    Square sq = null;       

    System.out.println("Enter the name of your data file (magicData.txt):");
    Scanner keyboard = new Scanner(System.in);
    String fileName = keyboard.nextLine();              // input data file name from keyboard

    Scanner inFile = new Scanner(new File (fileName));
    int sqSize = inFile.nextInt();                      // read the size 

    while (sqSize != -1)
    {
        sq = new Square(sqSize, inFile);
        if (sq.isMagicSquare())  //will return true or false
            System.out.println("\tWe have a Magic Square!");
        else
            System.out.println("\tThis is NOT a Magic Square.");

        System.out.println(sq);
        System.out.println();
        sqSize = inFile.nextInt();
    }

    System.out.println("Of the " + sq.getTotalTested() + " squares tested " + sq.getMagicCount() + " were magic square(s)" );
}
}

public class Square
{
    Scanner scan = new Scanner(System.in); //has been imported correctly, btw
    int tested = 0, areMagic = 0, sqSize;
    boolean magic;
    int[][] Square;

    public Square(int sqSize, Scanner inFile)
    {
        Square = new int [sqSize] [sqSize];
    }

    public void readSquare(Scanner inFile)
    {
        for(int row = 0; row < sqSize; row++)
            for(int col = 0; col < sqSize; col++)
            {
                Square[row][col] = inFile.nextInt();
                tested++;
            }
    }

    public boolean isMagicSquare(int[][] array)
    {
        Sums testMagic = new Sums();
        int rows = testMagic.sumRows(array);
        int cols = testMagic.sumCol(array);
        int diagonals = testMagic.sumDiagonal(array);

        if((rows == cols) && (cols == diagonals) && (diagonals == rows))
        {
            magic = true;
            areMagic++;
        }
        else 
            magic = false;
        return magic;
    }

    public int getMagicCount()
    {
        return areMagic;
    }

    public int getTotalTested()
    {
        return tested;     
    }
}



public class Sums
{
    int sum = 0, lastSum = 0, counter1, counter2, counter3;
    boolean magic = true;

    public int sumRows(int [][] array)
    {
        for (int row = 0; row < array.length; row++) 
        {
            sum = 0;
            for (int col = 0; col < array.length; col++) 
            {
                sum += array[row][col];
                System.out.print(sum + " ");
                if (lastSum == sum) 
                {
                    lastSum = sum;
                    counter1++;
                }
                else if (lastSum != sum) 
                    {
                        magic = false;
                        System.out.println("This is not a magic square");
                        row = array.length;
                        col = array.length;
                    }
            }
        }
        return counter1;
    }

    public int sumCol(int [][] array)
    {
        for (int col = 0; col < array.length; col++) 
        {
            sum = 0;
            for (int row = 0; row < array.length; row++) 
            {
                sum += array[row][col];
                System.out.print(sum + " ");
                if (lastSum == sum) 
                {
                    lastSum = sum;
                    counter2++;
                }
                else if (lastSum != sum) 
                    {
                        magic = false;
                        System.out.println("This is not a magic square");
                        row = array.length;
                        col = array.length;
                    }
            }  
        }
        return counter2;
    }

    public int sumDiagonal(int [][] array)
    {
        int diagonal1 = 0, diagonal2 = 0;
        for (int col = 0; col < array.length; col++) 
        {
            sum = 0;
            for (int row = 0; row < array.length; row++) 
            {
                if(row == col)
                {
                    sum += array[row][col];
                    System.out.print(sum + " ");
                    diagonal1 = sum;
                }
            }
        }
        for (int col = 0; col < array.length; col--) 
        {
            sum = 0;
            for (int row = 0; row < array.length; row++) 
            {
                if((row + col) == array.length - 1)
                {
                    sum += array[row][col];
                    System.out.print(sum + " ");
                    diagonal2 = sum;
                }
            }
        }

        if(diagonal1 == diagonal2)
        {
            magic = true;
            counter2 = counter3;
        }
        else
            counter3 = 0;

        return counter3;
    }
}

同样,如果我的代码格式看起来很奇怪,我们深表歉意。我以前从未在这里发布过,我正在尽力而为。

1 个答案:

答案 0 :(得分:0)

我建议您使用java.io.file并将平方和解析操作与文件读取操作分开。 可能如下所示:

public class MathSquareTester {

    public static void main (String[] args) throws IOException {
        List<Integer> squaredNums = new ArrayList<Integer>();
        FileInputStream in = new FileInputStream(file_name);
        BufferedReader reader = new BufferedReader(new FileReader(in));

        String line = reader.readLine();

        while (line != null) {
            //Assuming each line represents a separate integer

            squaredNums.add((int)Math.pow(Integer.parseInt(line)), 2);
            line = reader.readLine();
        }

        int[] numsArray = new int[squaredNums.size];
        squaredNums.toArray(numsArray);
        //All of your numbers are now stored in numsArray
    }
}