2016-07-31 39 views
1

我有下面的代碼,當我從控制檯傳遞這些參數時,它完美地工作。Java正則表達式不適用於第二行的輸入字符串

測試用例

{"012.99 008.73","099.99 050.00","123.45 101.07"} 

源代碼

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Pass the parameters"); 
String line=br.readLine(); 
String str=line.replaceAll("[^0-9 A-Z a-z /, .]",""); 
String[] nos=str.split(","); 

for(String s:nos){ 
    System.out.print(s+"\t"); 
} 

但是,當我從控制檯通過以下參數上面的代碼不工作。

{"612.72 941.34","576.46 182.66","787.41 524.70","637.96 333.23","345.01 219.69", 
"567.22 104.77","673.02 885.77"} 

字符串數組編號是錯過了在第二行中的字符串「567.22 104.77」,「673.02 885.77」。

請幫我解決這個問題。

+4

輸入的其餘部分是在第二行..你只是讀出一個行 – TheLostMind

+0

'字符串TMP,線=「」; while((tmp = br.readLine())!= null)line + = tmp;'應該爲你做,而不是你的代碼的第三行。 –

回答

1

它不工作,因爲你只讀第一行。

在這裏,您需要讀取字符串中的所有行。然後使用正則表達式。

BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("Pass the parameters"); 
String line; 

StringBuffer sb = new StringBuffer(""); 
while ((line = br.readLine()) != null) { 
    sb.append(line); 
} 
line = sb.toString(); 

String str=line.replaceAll("[^0-9 A-Z a-z /, .]",""); 
String[] nos=str.split(","); 

for(String s:nos){ 
    System.out.print(s+"\t"); 
} 
+0

ps:接受答案,如果它解決了你的問題[點擊正確的標記] – RAVI

相關問題