2014-03-07 31 views
0

我從文件中讀取,輸入是這樣的: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中的正則表達式解析,所以我不知道如何實現這一點。

還是有一個簡單的,更好的方法來做到這一點?

回答

1

這將數字存儲到Double -array(不是二維數組,Double對象的數組),因爲有些數字有.#。 int-arrays將消除後小數部分。

它使用正則表達式\b([\d.]+)\b來查找每個paren-group中的每個數字,並將每個數字加入到ArrayList<Double>中。需要注意的是它假定所有的輸入是完美的(沒有像(bogus,3.2)然後列表被翻譯成Double對象的數組

這應該給你朝着自己的目標的良好開端

import java.util.Arrays; 
import java.util.ArrayList; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

/** 
    <P>{@code java DoubleInParenStringsToArrays}</P> 
**/ 
public class DoubleInParenStringsToArrays { 
    public static final void main(String[] ignored) { 
     String input = "(1.0,2.0) (2,7.6) (2.1,3.0)"; 
     String[] inputs = input.split(" "); 

     //"": Dummy string, to reuse matcher 
     Matcher mtchrGetNums = Pattern.compile("\\b([\\d.]+)\\b").matcher(""); 
     for(String s : inputs) { 

     ArrayList<Double> doubleList = new ArrayList<Double>(); 
     mtchrGetNums.reset(s); 
     while(mtchrGetNums.find()) { 
      //TODO: Crash if it's not a number! 
      doubleList.add(Double.parseDouble(mtchrGetNums.group(0))); 
     } 

     Double[] doubles = doubleList.toArray(new Double[doubleList.size()]); 
     System.out.println(Arrays.toString(doubles)); 
     } 
    } 
} 

輸出:。

[C:\java_code\]java DoubleInParenStringsToArrays 
[1.0, 2.0] 
[2.0, 7.6] 
[2.1, 3.0] 
+0

謝謝。這就是我所尋找的相似之處。我可以從這裏拿走它。 – user3025403

+0

優秀。樂意效勞。並感謝您的贊成和接受。 – aliteralmind

1

如何每個項目解析:

Double.parseDouble( 「您的字符串這裏」);

至於存儲,我沒有得到你想要存儲你的值的模式。你爲什麼想要1.0,2,2.1和2.1的陣列,2.0,7.6和3.0到另一個的原因是什麼?

+0

後每行的描述是一系列的對。我想將它們分開和對所有第一值存儲在一個數組(實際上,列表,因爲我不知道大小),然後將所有第二個值轉換爲另一個數組)。感謝您的答案btw。 – user3025403

+0

但爲什麼2.1去第一個數組而3.0去第二個數組?如果我有這個描述(1.0,2.0)(2,7.6)(2.1,3.0)(5,6),會發生什麼?我似乎無法將你的邏輯存儲到數組中。 –

1

只要執行Integer.parseInt(string)或Double.parseDouble(string),然後將這些添加到數組中。不過,我並不十分確定你在問什麼。

0

我會用一個字符串Tokenizer.But需要全IMPL更多的信息和思想。

這爲y我們的行:「說明(1.0,2.0)(2,7.6)(2.1,3.0)」

第一件事 - 可以有沒有括號的情況?會不會有2,2,2組數字?

您是否想在每個號碼處理錯誤,或只是跳過線路或在出現錯誤時跳過處理(如號碼數量不匹配?)。

現在你需要一個數據結構來保存數字。如果每個數字在域中具有不同的含義,或者如果要將它們視爲簡單的數字列表,則可以創建一個類以將每個單獨元素保存在單獨的屬性中,或者具有數組列表或簡單數組。如果一個類一個樣本(incopmplete):

類的LineItem {

}

我們實際上打破了字符串有很多方法可以做到這一點。實際上取決於數據的質量,要如何應對可能出現的錯誤

一種方法是找到的第一個左括號(拍弦的休息和解析出使用字符串標記

喜歡的東西:。

int i = str.indexOf("("); 
String s2 = str.substring(i); 
StringTokenizer st = new StringTokenizer(s2, "() ,";//parenthesis, comma and space 

ArrayList<Double> lineVals1 = new ArrayList<Double>(); 
ArrayList<Double> lineVals1 = new ArrayList<Double>(); 
int cnt = 0; 
while(st.hasMoreTokens()){ 
    cnt++;//use this to keep count of how many numbers you got in line and raise error if need be 
    String stemp = st.nextToken(); 
    if(isNumeric(stemo)){ 
     if(cnt % 2 == 1){ 
      lineVals1.add(Double.parseDouble(stemp)); 
     }else{ 
      lineVals2.add(Double.parseDouble(stemp));  
     }  
    }else{ 
     /raise error if not numberic 
    } 
} 


public static boolean isNumeric(String str) 
{ 
    NumberFormat formatter = NumberFormat.getInstance(); 
    ParsePosition pos = new ParsePosition(0); 
    formatter.parse(str, pos); 
    return str.length() == pos.getIndex(); 
}