繁体   English   中英

如何从 .t​​xt 文件中读取数字并将它们放入二维数组中?

[英]How can I read numbers from a .txt file and put them into a 2-D array?

这是一个学校项目。 我需要从 .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;
    }
}

另外,如果我的代码看起来格式很奇怪,我深表歉意。 我以前从未在这里发过帖子,我正在尽我所能。

我建议您使用 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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM