繁体   English   中英

遍历并显示从文本文件获取的2D数组中的值

[英]Iterate through and show the values in a 2D array taken from a text file

嗨,我是2D数组的新手,但在显示数组索引的内容时遇到了问题。 我正在从文本文件中读取值,并将它们存储到2D数组中。 我不确定这些物品是否存储正确。 当我在填充2D数组的迭代过程中使用System.println()时,它会打印出所有当前显示的值。 但是,如果我尝试填充数组并在之后调用任何位置索引,例如data [45] [45]; 它只会返回0。应从文本文件中获取的每个应存储的int值均以百万为单位。

final String FILENAME = "DATA.TXT";

int [ ][ ] data = null;

int numberOfRows = 0;
int numberOfCols = 0;
String message ="";

try {
    File file = new File(FILENAME);
    Scanner inputFile = new Scanner(file);

    // Read the number of Rows and Columns first
    numberOfRows = inputFile.nextInt();
    numberOfCols = inputFile.nextInt();
    //Creates the row and column amount
    data = new int[numberOfRows][numberOfCols];

    for(int i = 0; i < data.length; i++)
    {
        for(int j = 0; j < data[5].length; j++)
        {
            while(inputFile.hasNextInt())
            {
                data[i][j] = inputFile.nextInt();
                //System.out.println(data[i][j]); //This works for displaying the values cuurently
            }
        }
    }
    inputFile.close();
    }
    catch (FileNotFoundException ex) 
    {
        System.out.println("Error reading data from " + FILENAME + 
        " Exception = " + ex.getMessage());
    }

    System.out.println("Data has been read - file has " + numberOfRows + 
    " rows and " + numberOfCols + " columns.");

//THIS or even using a loop to iterate through the 2D array that should contain 
// the values does not appear and only ends up as 0 as if the values are not stored.
System.out.print(data[56][56]);
}
}

我已经看了几个小时,我不知道这些值是否没有正确存储,或者是否有原因不能在任何数据索引中调用该值,例如data [45] [45]或使用诸如

for(int n =0; n <data.length; n++)
{
    for(int k = 0; k < data[0].length; k++)
    {
        System.out.print(data[n][k]);
    }
}

如果我这样做完全错误,或者有更有效的方法来执行此操作,请告诉我。 否则,一般来说我不知道​​出什么问题了。

感谢您检查我的帖子,我相信这将对许多新程序员提供帮助,帮助他们进行2D数组和文件读取。

文件内容

500 1000 1052662 1025260 1064342 1045596 1093363 1063663 1014129 1070544 1005352 1046317 1059536 1009817 1049327 1012134 1047499 1026392 1056558 1098823 1060554 1028017 1046977 1022098 1018538 1077771 1082687 1025653 1056869 1076473 1097420 1080444 106379710147654

尝试这个 :

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Test {


    public static void main(String[] args) {

        final String FILENAME = "d:/data.txt";

        long[][] data=null;

        int numberOfRows = 0;
        int numberOfCols = 0;
        String message = "";

        try {
            File file = new File(FILENAME);
            Scanner inputFile = new Scanner(file);

            // Read the number of Rows and Columns first
            numberOfRows = inputFile.nextInt();
            numberOfCols = inputFile.nextInt();
            // Creates the row and column amount
            data = new long[numberOfRows][numberOfCols];

            for (int i = 0; i < numberOfRows; i++) {
                for (int j = 0; j < numberOfCols; j++) {

                        data[i][j] = inputFile.nextInt();


                }
            }
            inputFile.close();
        } catch (FileNotFoundException ex) {
            System.out.println("Error reading data from " + FILENAME
                    + " Exception = " + ex.getMessage());
        }

        System.out.println("Data has been read - file has " + numberOfRows
                + " rows and " + numberOfCols + " columns.");

    // display 2D array

        for(int n =0; n <numberOfRows; n++)
        {
            for(int k = 0; k < numberOfCols; k++)
            {
                System.out.print(data[n][k] +"  ");
            }
            System.out.println();
        }
    }

}

发现我只需要摆脱while语句,它似乎运行得很好。 多谢您的协助。

暂无
暂无

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

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