Java:如何有选择地将文件扫描到两个2d字符串数组中

Java:如何有选择地将文件扫描到两个2d字符串数组中,java,arrays,java.util.scanner,Java,Arrays,Java.util.scanner,如何有选择地将文件扫描到两个二维字符串数组中 我有一个文件如下所示: 2014 fruit apple red 50 30 2015 vegetable spinach green 10 2014 fruit orange orange 100 2014 fruit banana yellow 90 100 2015 vegetable corn yellow 120 我试图读取文件并将每一行放入一个二维字符串数组(由空格分隔)。我想要一个阵列,由2014年开始的线条组成,第二个阵列由2015

如何有选择地将文件扫描到两个二维字符串数组中

我有一个文件如下所示:

2014 fruit apple red 50 30
2015 vegetable spinach green 10
2014 fruit orange orange 100
2014 fruit banana yellow 90 100
2015 vegetable corn yellow 120
我试图读取文件并将每一行放入一个二维字符串数组(由空格分隔)。我想要一个阵列,由2014年开始的线条组成,第二个阵列由2015年开始的线条组成

因为它是针对一个类的,所以我必须使用严格的参数1。它必须来自文本文件,并且为2。它必须进入两个2d阵列

我能够成功地将整个文件读入一个2d字符串数组,但我尝试两个数组的所有操作似乎都失败了。这是我得到的。。。 成功的单个二维阵列:

public static void main(String[] args) throws Exception {

    //Initializes the variables needed to read the line length of the file
    FileReader fr = null;
    LineNumberReader lnr = null;
    File newFile = new File("ProjectTest.txt");

    //Initiallizes the variables needed to create the 2D array
    BufferedReader inputStream = new BufferedReader(new FileReader(newFile));
    Scanner scannerIn = new Scanner(inputStream);

    //Other variables
    int lineNum = 0;
    int linesCounter = 0;
    String str;

    //Determines how many lines in the file
    try {
        fr = new FileReader(newFile);
        lnr = new LineNumberReader(fr);
        while (lnr.readLine() != null) {
            lineNum++;
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    } finally {
        if (fr != null) {
            fr.close();
        }
        if (lnr != null) {
            lnr.close();
        }
    }

//Create the 2D array
    String arrayA[][] = new String[lineNum][];

    while (scannerIn.hasNextLine() && linesCounter < lineNum) {
        arrayA[linesCounter] = scannerIn.nextLine().split("\\s");
       linesCounter++;
    }

//Test-print the array
for (int i = 0; i< lineNum; i++){
         System.out.println(arrayA[i][2]);
     }
}

请帮帮我!!谢谢大家

这里有一种方法:

String[][] arrayA = new String[lineNum][];
String[][[ arrayB = new String[lineNum][];

int counterA = 0;
int counterB = 0;

while (scannerIn.hasNextLine()) {
    String line = scannerIn.nextLine();
    String[] parts = line.split("\\s");
    if (line.startsWith("2014")) {
        arrayA[counterA++] = parts;
    } else if (line.startsWith("2015")) {
        arrayB[counterB++] = parts;
    }
}

// prune excess elements
arrayA = Arrays.copyOf(arrayA, counterA);
arrayB = Arrays.copyOf(arrayB, counterB);

在运行if-else语句之前,请尝试拆分字符串。那么你就有了一个目标

tempString[0]

要与日期进行比较,一种方法是:

    File fin = new File("your_file_path");
    FileInputStream fis = new FileInputStream(fin);

    //Construct BufferedReader from InputStreamReader
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));
        String line = null;
        int lineNum = 1024;
        String [][] linesWith2014 = new String[lineNum][];
        String [][] linesWith2015 = new String[lineNum][];
        int countWith2014 = 0 ;
        int countWith2015 = 0;

        while ((line = br.readLine()) != null) {
            System.out.println(line);
            if (line.startsWith("2014")){
                linesWith2014[countWith2014++] = line.split("\\s+");
            }else if (line.startsWith("2015")){
                linesWith2015[countWith2015++] = line.split("\\s+");
            }
        }

        br.close();

        linesWith2014 = Arrays.copyOf(linesWith2014, countWith2014);
        linesWith2015 = Arrays.copyOf(linesWith2015, countWith2015);

当然,在它周围还有try-catch。

在大多数情况下,最好在集合中收集数据,而不是在数组中收集数据

我只需要创建两个数组列表,例如

ArrayList<ArrayList<String>>
ArrayList
然后一行一行地填。不需要提前计算任何东西

然后简单地从列表中创建数组,这很容易,因为大小已知:)

单个列表的示例:

List<List<String>> list = new ArrayList<>();

// fill the list of lists...

// convert the list of lists to a 2D array...
String[][] array = new String[list.size()][];

int index = 0;
for (List<String> sublist : list)
{
    array[index] = sublist.toArray(new String[sublist.size()]);
    ++index;
}
List List=new ArrayList();
//填写列表列表。。。
//将列表列表转换为二维数组。。。
字符串[][]数组=新字符串[list.size()][];
int指数=0;
for(列表子列表:列表)
{
数组[index]=sublist.toArray(新字符串[sublist.size()]);
++指数;
}

现实世界的解决方案是使用现成的CSV阅读器,例如apache commons。我必须将字符串部分更改为字符串[]部分才能使其工作,但这个解决方案非常完美!谢谢你的出色表现。哈哈,谢谢@Sharlabean指出问题所在(在电话上打字),现在修好了。这个解决方案也非常有效,而且非常有意义。看着你的代码…我离你太近了!(停止撞墙)我需要多挣几分,这样我就可以回来给你投票了。
List<List<String>> list = new ArrayList<>();

// fill the list of lists...

// convert the list of lists to a 2D array...
String[][] array = new String[list.size()][];

int index = 0;
for (List<String> sublist : list)
{
    array[index] = sublist.toArray(new String[sublist.size()]);
    ++index;
}