nikthecamel
nikthecamel

Reputation: 45

Java regex not working for the input string in the second line

I have the below code which works perfectly when I pass these parameters from the console.

Test-case

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

Source code

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");
}

But the above code does not work when i pass the below parameters from the console.

{"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"}

The String array nos is missing out the strings "567.22 104.77","673.02 885.77" in the second line.

Please help me on this.

Upvotes: 1

Views: 95

Answers (1)

RAVI
RAVI

Reputation: 3153

It is not working because you are reading only first line.

Here, You need to read all line in a string. Then use regex on it.

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");
}

Upvotes: 1

Related Questions