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

Java 如何从.txt文件中读取数字并将其放入二维数组中?,java,Java,这是一个学校项目。我需要从.txt文件中读取数字并将其放入数组中。当它们在一个数组中之后,我需要将其传递到另一个类中进行数学运算并比较这些数字。唯一的问题是,我无法获取读取.txt文件或将其放入数组的代码 我需要使用 if (sq.isMagicSquare()) 要将数组传递给类Square,但会产生错误: required: int[][] found: no arguments reason: actual and formal lists differ in length

这是一个学校项目。我需要从.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;
    }
}
必填项:int[]
找到:没有参数
原因:实际清单和正式清单长度不同
公共级魔术师
{
公共静态void main()引发IOException
{
平方平方=零;
System.out.println(“输入数据文件的名称(magicData.txt):”;
扫描仪键盘=新扫描仪(System.in);
String fileName=keyboard.nextLine();//从键盘输入数据文件名
扫描仪内嵌=新扫描仪(新文件(文件名));
int sqSize=infle.nextInt();//读取大小
while(sqSize!=-1)
{
sq=新正方形(sqSize,填充);
if(sq.isMagicSquare())//将返回true或false
System.out.println(“\t我们有一个魔方!”);
其他的
System.out.println(“\t这不是幻方”);
系统输出打印项次(平方);
System.out.println();
sqSize=infle.nextInt();
}
System.out.println(“测试的“+sq.getTotalTested()+”方块“+sq.getMagicAccount()+”是幻方)”;
}
}
公共课广场
{
Scanner scan=新扫描仪(System.in);//已正确导入,顺便说一句
int tested=0,areMagic=0,sqSize;
布尔魔法;
int[][]平方;
公共广场(int sqSize,扫描仪填充)
{
Square=新整数[sqSize][sqSize];
}
公共空白readSquare(扫描仪填充)
{
for(int row=0;row

另外,如果我的代码看起来格式怪异,我也会道歉。我以前从未在这里发表过文章,我正在尽我所能。

我建议您使用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
    }
}
公共类MathSquareTester{
公共静态void main(字符串[]args)引发IOException{
List squaredNums=new ArrayList();
FileInputStream in=新的FileInputStream(文件名);
BufferedReader reader=新的BufferedReader(新文件读取器(in));
字符串行=reader.readLine();
while(行!=null){
//假设每条线都代表