【发布时间】:2014-03-07 04:56:55
【问题描述】:
我正在从文件中读取,输入是这样的:
Description (1.0,2.0) (2,7.6) (2.1,3.0)
Description2 (4,1)
...
Description_n (4,18) (8, 7.20)
我希望能够获取括号内的数字并使用将它们从字符串转换为数字,以便我可以对它们进行数学运算。现在,为了简化事情,我的代码只读取第一行,然后根据空格将其拆分:
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(new File("filename.txt")));
//reader reads in the first line
String firstLine = reader.readLine();
//splits into an array of ["Description","(1.0,2.0)","(2,7.6)","(2.1,3.0)"]
String[] parts = first.split(" ");
//now I want to store 1.0, 2, and 2.1 in one array as ints and 2.0, 7.6, and 3.0 in another int array
} catch (Exception e) {
System.exit(0);
}
有哪些方法可以将括号内的数字存储到两个单独的整数数组中(参见上面的评论)?我是否应该使用正则表达式以某种方式捕获“([1-9.],[1-9.])”形式的某些内容,然后将它们传递给另一个函数,该函数然后将一对中的第一个数字与第二个数字分开,然后将它们都转换为整数?我是 Java 中正则表达式解析的新手,所以我不确定如何实现它。
或者有没有更简单、更好的方法来做到这一点?
【问题讨论】: