我將讀取文件內容作爲我的程序的輸入。從文件中讀取並將它們解析到不同的數組中
我將它存儲到一個字符串中,並將它拆分爲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個數組的類的對象併爲其賦值。
但是,有沒有更好的方法來做到這一點?
他們不在方括號內。 –
我在下面添加了輸入和輸出。 –
根據您的輸入/輸出數據修改我的答案 –