从文件中读取并将它们解析为不同的数组
Read from file and parse them into different arrays
我将读取文件内容作为我程序的输入。
我将它存储到一个字符串中并将其拆分为 2 个字符串。
File file = new File("question.txt");
PrintWriter pw = new PrintWriter(file);
pw.write("81 : (1,53.38,) (2,88.62,) (3,78.48,) (4,72.30,) (5,30.18,) (6,46.34,)");
pw.close();
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
Map<Integer, Object> map = new HashMap<Integer, Object>();
int i = 0;
Demo d = new Demo();
String[] part = line.split(":");
String part1 = part[0].trim();
String part2 = part[1].trim();
输入: 81 : (1,53.38,) (2,88.62,) (3,78.48,) (4,72.30,) (5,30.18,) (6,46.34,)
输出:
totalWeight = 81;
index[] = {1,2,3,4,5,6};
weight[] = {53.38,88.62,78.48,72.30,30.18,46.34};
value[] = {45,98,3,76,9,48};
第二个字符串包含括号内的 3 个值。第一个数字是 "Index",第二个数字是 "Weight",第三个数字是 "Value".
我如何将它拆分并存储到 3 个不同的数组中?
我知道一种方法是将其解析为 class 的对象,该对象具有 3 个数组并为它们赋值。
但是有没有更好的方法呢?
String[] part2 = part[1].replace("(", "").trim().split(")");
List<String> indices = new ArrayList<String>();
List<String> weights= new ArrayList<String>();
List<String> values = new ArrayList<String>();
for (String s : part2)
{
String item = s.split(",");
indices.add(item[0]);
weights.add(item[1]);
values.add(item[2]);
}
// to get them as arrays
String[] allWeightsArray = weights.toArray(new String[]); ...
使用此代码解析此格式的字符串:
String[] structured = part2.replaceAll("[()]", "").split(" ");
List<String> indexes = new ArrayList<>();
List<String> weights = new ArrayList<>();
List<String> values = new ArrayList<>();
for (String elem : structured) {
String[] parsed = elem.split(",");
indexes.add(parsed[0]);
weights.add(parsed[1]);
values.add(parsed[2]);
}
使用split
方法将字符串拆分为[\(?+\)]+\s+
String x = part[1].trim();
String[] items = x.split("[\(?+\)]+\s+");
List<String> indexs = new ArrayList<>();
List<String> wights = new ArrayList<>();
List<String> values = new ArrayList<>();
for(String item : items)
{
item = item.replace("(" , "").replace(")" , "").trim();
String[] partItems = item.split(",");
indexs.add(partItems[0]);
wights.add(partItems[1]);
values.add(partItems[2]);
}
System.out.println(Arrays.toString(indexs.toArray()));
System.out.println(Arrays.toString(wights.toArray()));
System.out.println(Arrays.toString(values.toArray()));
输出
[1, 2, 3, 4, 5, 6]
[53.38, 88.62, 78.48, 72.30, 30.18, 46.34]
[, , , , , ]
您可以使用这样的模式解决您的问题:
String str = "81 : (1,53.38,) (2,88.62,) (3,78.48,) "
+ "(4,72.30,) (5,30.18,) (6,46.34,)";
String regex = "\((\d+),(\d+\.\d+),([^\d](\d+))\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
double totalWeight = Double.parseDouble(str.replaceAll("^(\d+).*", ""));
List<Integer> indexes = new ArrayList<>();
List<Double> weights = new ArrayList<>();
List<Integer> valuess = new ArrayList<>();
while (matcher.find()) {
indexes.add(Integer.parseInt(matcher.group(1)));
weights.add(Double.parseDouble(matcher.group(2)));
valuess.add(Integer.parseInt(matcher.group(4)));
}
System.out.println(totalWeight);// 81.0
System.out.println(indexes); // [1, 2, 3, 4, 5, 6]
System.out.println(weights); // [53.38, 88.62, 78.48, 72.3, 30.18, 46.34]
System.out.println(valuess); // [45, 98, 3, 76, 9, 48]
- 总重量是输入中的第一个数字,您只需使用 replaceAll 即可获得它,然后解析输入
Double.parseDouble(str.replaceAll("^(\d+).*", ""));
- 要获取其余信息,您可以使用此正则表达式
\((\d+),(\d+\.\d+),([^\d](\d+))\)
(regex demo),它可以匹配两个 ()
之间的三个不同数字,在您的情况下,您有很多结果你可以使用模式。
我将读取文件内容作为我程序的输入。
我将它存储到一个字符串中并将其拆分为 2 个字符串。
File file = new File("question.txt");
PrintWriter pw = new PrintWriter(file);
pw.write("81 : (1,53.38,) (2,88.62,) (3,78.48,) (4,72.30,) (5,30.18,) (6,46.34,)");
pw.close();
BufferedReader br = new BufferedReader(new FileReader(file));
String line = br.readLine();
Map<Integer, Object> map = new HashMap<Integer, Object>();
int i = 0;
Demo d = new Demo();
String[] part = line.split(":");
String part1 = part[0].trim();
String part2 = part[1].trim();
输入: 81 : (1,53.38,) (2,88.62,) (3,78.48,) (4,72.30,) (5,30.18,) (6,46.34,)
输出:
totalWeight = 81;
index[] = {1,2,3,4,5,6};
weight[] = {53.38,88.62,78.48,72.30,30.18,46.34};
value[] = {45,98,3,76,9,48};
第二个字符串包含括号内的 3 个值。第一个数字是 "Index",第二个数字是 "Weight",第三个数字是 "Value".
我如何将它拆分并存储到 3 个不同的数组中?
我知道一种方法是将其解析为 class 的对象,该对象具有 3 个数组并为它们赋值。
但是有没有更好的方法呢?
String[] part2 = part[1].replace("(", "").trim().split(")");
List<String> indices = new ArrayList<String>();
List<String> weights= new ArrayList<String>();
List<String> values = new ArrayList<String>();
for (String s : part2)
{
String item = s.split(",");
indices.add(item[0]);
weights.add(item[1]);
values.add(item[2]);
}
// to get them as arrays
String[] allWeightsArray = weights.toArray(new String[]); ...
使用此代码解析此格式的字符串:
String[] structured = part2.replaceAll("[()]", "").split(" ");
List<String> indexes = new ArrayList<>();
List<String> weights = new ArrayList<>();
List<String> values = new ArrayList<>();
for (String elem : structured) {
String[] parsed = elem.split(",");
indexes.add(parsed[0]);
weights.add(parsed[1]);
values.add(parsed[2]);
}
使用split
方法将字符串拆分为[\(?+\)]+\s+
String x = part[1].trim();
String[] items = x.split("[\(?+\)]+\s+");
List<String> indexs = new ArrayList<>();
List<String> wights = new ArrayList<>();
List<String> values = new ArrayList<>();
for(String item : items)
{
item = item.replace("(" , "").replace(")" , "").trim();
String[] partItems = item.split(",");
indexs.add(partItems[0]);
wights.add(partItems[1]);
values.add(partItems[2]);
}
System.out.println(Arrays.toString(indexs.toArray()));
System.out.println(Arrays.toString(wights.toArray()));
System.out.println(Arrays.toString(values.toArray()));
输出
[1, 2, 3, 4, 5, 6]
[53.38, 88.62, 78.48, 72.30, 30.18, 46.34]
[, , , , , ]
您可以使用这样的模式解决您的问题:
String str = "81 : (1,53.38,) (2,88.62,) (3,78.48,) "
+ "(4,72.30,) (5,30.18,) (6,46.34,)";
String regex = "\((\d+),(\d+\.\d+),([^\d](\d+))\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
double totalWeight = Double.parseDouble(str.replaceAll("^(\d+).*", ""));
List<Integer> indexes = new ArrayList<>();
List<Double> weights = new ArrayList<>();
List<Integer> valuess = new ArrayList<>();
while (matcher.find()) {
indexes.add(Integer.parseInt(matcher.group(1)));
weights.add(Double.parseDouble(matcher.group(2)));
valuess.add(Integer.parseInt(matcher.group(4)));
}
System.out.println(totalWeight);// 81.0
System.out.println(indexes); // [1, 2, 3, 4, 5, 6]
System.out.println(weights); // [53.38, 88.62, 78.48, 72.3, 30.18, 46.34]
System.out.println(valuess); // [45, 98, 3, 76, 9, 48]
- 总重量是输入中的第一个数字,您只需使用 replaceAll 即可获得它,然后解析输入
Double.parseDouble(str.replaceAll("^(\d+).*", ""));
- 要获取其余信息,您可以使用此正则表达式
\((\d+),(\d+\.\d+),([^\d](\d+))\)
(regex demo),它可以匹配两个()
之间的三个不同数字,在您的情况下,您有很多结果你可以使用模式。