从Java文本文件中读取矩阵

问题描述 投票:-2回答:1

我正在尝试阅读以下文本文件:

## k - number of resources; the next k lines
2
5
5
## n - number of treatment patterns; (matrix [n-1][k])
3
2.2 2.6
2.6 2.2
## the next k lines - desired utilization level for each resource N_j
4
4

我正在寻找输出

resources = [2,2,5]
treatmentPatterns = [[3],[2.2,2.6],[2.6,2.2]]
utilisation = [4,4]

我试图使资源以这种方式工作,但是不起作用。

        while(scan.hasNextLine()) {
            String read = scan.nextLine();
            if(read.contains("##")) {
                String read2 = scan.nextLine();
                while(!read2.contains("##")) {;
                    //add read2 to a new list
                    read2 = scan.nextLine();
                }
            }
        }
java filereader
1个回答
0
投票

基于对问题的分析,您可以执行以下操作:

int myArrayIndex = -1;
while (scan.hasNextLine()) {
    String read = scan.nextLine();               // get next line
    if(read.contains("##")) { myArrayIndex++; }  // switch to next array-key
    else {                                       // next value found
// use myArrayIndex as key for your arrays and create your array
// e.g. switch(myArrayIndex) { case 0: do_resources; break; ... }     
   }
}
© www.soinside.com 2019 - 2024. All rights reserved.