我将读取文件内容作为我的程序的输入。从文件中读取并将它们解析到不同的数组中
我将它存储到一个字符串中,并将它拆分为2个字符串。
File file = new File("question.txt");
PrintWriter pw = new PrintWriter(file);
pw.write("81 : (1,53.38,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)");
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,$45) (2,88.62,$98) (3,78.48,$3) (4,72.30,$76) (5,30.18,$9) (6,46.34,$48)
输出:
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个不同的数组中?
我知道这样做的一种方法,将其解析为具有3个数组的类的对象并为其赋值。
但是,有没有更好的方法来做到这一点?
他们不在方括号内。 –
我在下面添加了输入和输出。 –
根据您的输入/输出数据修改我的答案 –