gpt4 book ai didi

java - 将文件中的每一行读入其自己的不同二维数组中

转载 作者:行者123 更新时间:2023-11-30 05:21:06 25 4
gpt4 key购买 nike

我想用 Java 读取文件。复杂之处在于将其管理到自己的二维数组中。该文件的行如下:

#  1  1    1  2    1  3    1  4    1  5    2  3      8   8      9   7    18  17 
* 2 4 2 5 2 6 2 7 2 8 18 18 10 11 16 18
HRow 5 5 20 3
VRow 6 6 10 4

空格是对之间的分隔符。配对内有 2 个空格,配对之间有 3 个空格。

输出应该是:

private static final int[][] HASH_LOCATIONS = {{1,1},{1,2},{1,3},{1,4},{1,5},{2, 3}, {8, 8}, {9, 7}, {17, 18}};
private static final int[][] AST_LOCATIONS = {{2, 4}, {2, 5}, {2, 6}, {2, 7}, {2, 8}, {18, 18}, {10, 11}, {16, 18}};
private static final int [][] H_ROW = {{5,5,20,3}};
private static final int [][] V_ROW = {{6,6,10,4}};

我尝试过以下方法:

       try {
FileInputStream fileIn = new FileInputStream(fileName);
Scanner scanner = new Scanner(fileIn);
while (scanner.hasNext()){
String itemType = scanner.next();
if (itemType.equals("#")) {
ArrayList<Integer> hashLocations = new ArrayList<>();
while(scanner.hasNextInt()){
hashLocations.add(scanner.nextInt());
}
} else if (itemType.equals("*")) {
ArrayList<Integer> astLocations = new ArrayList<>();
while(scanner.hasNextInt()){
astLocations.add(scanner.nextInt());
}
} else if (itemType.equals("HRow")) {
ArrayList<Integer> hLocations = new ArrayList<>();
while(scanner.hasNextInt()){
hLocations.add(scanner.nextInt());
}
} else if (itemType.equals("VRow")) {
ArrayList<Integer> vLocations = new ArrayList<>();
while(scanner.hasNextInt()){
vLocations.add(scanner.nextInt());
}
}
}
return true;
} catch (FileNotFoundException ex) {
return false;
}

我不知道如何管理这个问题。任何帮助,将不胜感激。

最佳答案

我会逐行阅读而不是逐个字符阅读。对于每一行,如果您知道对的数量(例如:预期的数量),我会计算它们以确保它们没问题。

如果文件在对前面有这些定义(例如:#、* 等),那么我将确保将它们放入正确的“存储桶”中。我将为此使用 map ,因此它可以很容易地添加新值。但如果你只想限制这些并炸掉其他任何东西,那么这也可以实现。

另请注意,输入文件中的某些空格可能是错误的,除非您的定义错误,所以请检查它。

请参阅下面的一些示例以帮助您入门。这是基于附加的示例输入(除了遵循间距规则之外,这正是您的输入)

public static void main(String[] args) {
final int EMPTY_COUNT_BETWEEN_TUPLES = 2; // after split
List<String> allowedRowTypes = Arrays.asList("*", "#", "HRow", "VRow");
Map<String, List<List<Integer>>> buckets = new HashMap<>();


BufferedReader reader;
try {
reader = new BufferedReader(new FileReader("C:\\Users\\e048222\\Playground\\stackoverflowPlay\\src\\com\\company\\input.txt"));
String line = reader.readLine();
while(line != null) {
System.out.println(line);
String[] tokens = line.split(" ");

if(tokens.length == 0) {
throw new IllegalArgumentException("Invalid line");
}
String rowType = tokens[0];
if(!allowedRowTypes.contains(rowType)) {
throw new IllegalArgumentException("Invalid line type");
}
List<Integer> tuple = null;
int countEmpty = 0;
List<List<Integer>> bucketValues = buckets.get(rowType);
for(int tokenCount=1; tokenCount<tokens.length; tokenCount++) {
String value = tokens[tokenCount];
if(value.isEmpty()) {
countEmpty++;
} else {
if(countEmpty == EMPTY_COUNT_BETWEEN_TUPLES) {
bucketValues = addToBucket(tuple, bucketValues);
tuple = null;
}

if(tuple==null) {
tuple = new ArrayList<>();
}

tuple.add(Integer.parseInt(value));
countEmpty = 0;
}
}
bucketValues = addToBucket(tuple, bucketValues);
buckets.put(rowType, bucketValues);
System.out.println(bucketValues);

line = reader.readLine();
}


} catch (IOException e) {
System.out.println(e.getMessage());
}

System.out.println(buckets);
}

private static List<List<Integer>> addToBucket(List<Integer> tuple, List<List<Integer>> bucketValues) {
if (bucketValues == null) {
bucketValues = new ArrayList<>();
}
bucketValues.add(tuple);
return bucketValues;
}

更新的输入文件:

# 1  1   1  2   1  3   1  4   1  5   2  3   8  8   9  7   18  17
* 2 4 2 5 2 6 2 7 2 8 18 18 10 11 16 18
HRow 5 5 20 3
VRow 6 6 10 4

输出:

# 1  1   1  2   1  3   1  4   1  5   2  3   8  8   9  7   18  17
[[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [8, 8], [9, 7], [18, 17]]
* 2 4 2 5 2 6 2 7 2 8 18 18 10 11 16 18
[[2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [18, 18], [10, 11], [16, 18]]
HRow 5 5 20 3
[[5, 5, 20, 3]]
VRow 6 6 10 4
[[6, 6, 10, 4]]
{#=[[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [8, 8], [9, 7], [18, 17]], HRow=[[5, 5, 20, 3]], *=[[2, 4], [2, 5], [2, 6], [2, 7], [2, 8], [18, 18], [10, 11], [16, 18]], VRow=[[6, 6, 10, 4]]}

不是最干净的代码,但可以让你的创意源源不断。对于输入文件的几乎任何结构更改来说,这都会失败,但可以进行很多改进。

关于java - 将文件中的每一行读入其自己的不同二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59557217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com