gpt4 book ai didi

Java:解析输入文件并提取列值

转载 作者:行者123 更新时间:2023-12-01 14:07:08 25 4
gpt4 key购买 nike

这是一个示例输入文本文件:

some text
one more line of text
another line
and one more
1 23 3
2 34 4
5 12 4
3 23 8

我需要提取每一列数值并对其执行一些数学运算。

这是我到目前为止所尝试过的:

Path inputFile = Paths.get(args[0]);

try{
ArrayList<String> lines = (ArrayList<String>) Files.readAllLines(inputFile, Charset.defaultCharset());
ArrayList<String []> columns = new ArrayList<>();
for(String line : lines){
columns.add(line.split("\\s"));
}
// Now for each line you have columns.
for(String [] s : columns){
System.out.println(Arrays.toString(s));
}

但这就是不对,我无法摆脱这个僵局。任何帮助将不胜感激..

最佳答案

我会说...

for(String line : lines){
// Just add this condition
if(line.matches("^[0-9]"){
// then process the way you want to...
}
}

希望这有帮助...

编辑:

Path inputFile = Paths.get(args[0]);

try{
ArrayList<String> lines = (ArrayList<String>) Files.readAllLines(inputFile, Charset.defaultCharset());
ArrayList<String []> columns = new ArrayList<>();
for(String line : lines){
if(line.matches("^[0-9]")){
System.out.println("Line:"+line);
String[] colms = line.split("\\s+");
columns.add(colms);
for (int i = 0; i < colms.length; i++) {
String temp = colms[i];
System.out.println("Colmns:"+i+":"+temp);
// process temp one by one
}
}
}
}catch(Exception ex){
ex.printStackTrace();
System.out.println("Exception...");
}

关于Java:解析输入文件并提取列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18818974/

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