2015-06-23 82 views
-1

我打算讀取由逗號的示例下面如何編寫讀取由逗號

08090417241分隔的數字線,080904152335,080998766231,080913456786,08035467901, 08070415243,080675534387,080355525288分隔串的Java方法, 080345887698,0809966355343,...

到最後一個數字。

我在程序中使用的原代碼是這樣的

String file1 = FileArea6.getText(); 

當我嘗試用上面的例子來讀取數字這給出了一個錯誤。但是當數字按照下面的順序排列時,它會被完美地讀取。

08035467901 
08035474477 
08024444448 
08012233333 
09033222333....... 

什麼是正確的方式來閱讀使用第一個例子,即逗號分隔?

+1

什麼錯誤你好嗎?請你提供更多信息 – TheCodingFrog

回答

2

所有你需要的是str.split(',')

List<String> nums = Arrays.asList(str.split("\\s*,\\s*")); 

List<String> nums = Arrays.asList(str.split(",")); 

String[] nums = str.split(","); 
0

下面簡單的代碼片段應該工作。

  try { 
       BufferedReader bReader = new BufferedReader(new FileReader("input file")); 
       String line = ""; 
       while ((line = bReader.readLine()) != null) { 
        String[] strArray = line.split(","); 
        for (String str : strArray) { 
         System.out.println(str.trim()); 

         } 
        } 
       } 

      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
+0

你們都搖滾。謝謝 – Ribo01

0
String[] separateByComma(String s){ 
    String[] tmp = s.split(","); 
    int n = 0, i=0; 

    while(i<tmp.length){ 
     if(check(tmp[i])) tmp[n++] = tmp[i]; 
     i++; 
    }  
    if(n==0) return null; 

    String[] res = new String[n]; 

    while(--n >= 0) res[n] = tmp[n]; 
    return res; 
} 

/* 
* Your check function 
* You can filter anything 
*/ 
boolean check(String s){ 
    if(s==null || s.length()<1) return false; 
    return true; 
}