我從文件中讀取,輸入是這樣的:Java - 將字符串轉換爲數字的想法(RegEx/Parsing /等)?
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中的正則表達式解析,所以我不知道如何實現這一點。
還是有一個簡單的,更好的方法來做到這一點?
謝謝。這就是我所尋找的相似之處。我可以從這裏拿走它。 – user3025403
優秀。樂意效勞。並感謝您的贊成和接受。 – aliteralmind