1

I am having trouble removing new line character that appears in the line.

My input is:

1,john,a

2,smith,b

3,mike

,c

4,clark,d

My output should be:

1,john,a

2,smith,b

3,mike,c

4,clark,d

Below is the code I wrote so far. I am using "continue" to remove bad records based on count of expected "," in each line. However, I would like to concatenate the line where "\n" exists wrongly with the next line to get the line instead of removing as bad record.

String test="1,john,a\n2,smith,b\n3,mike\n,c\n4,clark,d";

String[] test2=test.split("\n");

int count=0;
String s="";
for(int i=0;i<=test2.length-1;i++)
{
    for(int j=0;j<test2[i].length();j++) {
        char c= test2[i].charAt(j);
        if(c==',') {
           count++;
           if(count<2) {
              continue;
           }
           System.out.println(test2[i]);
        }
    }
    count=0;    
}
13
  • Is your input guaranteed to have exactly three (desired) elements per line? Commented Jul 16, 2018 at 18:02
  • @markspace yes, it should always have 3 elements in a line Commented Jul 16, 2018 at 18:06
  • Does split() not remove the newline character? Commented Jul 16, 2018 at 18:06
  • FYI: Any attempt at sanitizing poorly-formatted data after the fact is likely to be flaky. If your input is allowing newlines, are you really certain it's not allowing commas? How do you know "3,mike\n,c\n4,clark,d" means [[3,"mike","c"],[4,"clark","d"]] and not [[3,"mike\n,c,4,clark","d"]]? If at all possible, the best solution is to change the way these values are getting encoded in the first place. Commented Jul 16, 2018 at 18:12
  • Use a regular expression to replace any occurrence of \n, or ,\n with "" Commented Jul 16, 2018 at 18:13

3 Answers 3

4

If you always have exactly three elements per line, try splitting on both \n and , and then just combine three elements into one line.

String test="1,john,a\n2,smith,b\n3,mike\n,c\n4,clark,d";

String[] test2=test.split("[\n,]+");

for(int i=0;i<test2.length;i+=3)
{
   System.out.println(test2[i]+","+test2[i+1]+","+test2[i+2]);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Could you just call text2.split() on the string? Won't this break on each newline without specifying the carriage? Just wondering, I dont have JDK on this machine and haven't used Java in some time.
I don't think this works, as test.split("[\n,]"); will produce an empty string between "mike" and "c". See docs.oracle.com/javase/8/docs/api/java/lang/…
@markspace Thanks fro the response. However, this code did not return the expected output
@StriplingWarrior I added a + after the bracket, works for me now. Sorry for posting untested code.
@mohammedahmed I added a + to the regex, try it now.
|
-1
String test="1,john,a\n2,smith,b\n3,mike\n,c\n4,clark,d";

String[] test2=test.split("\n");

int count=0;
String s="";
for(int i=0;i<=test2.length-1;i++)
{
   String[] temp=test2[i].split(",");
   if(temp.length!=3){
     s=s+test2[i]+test2[i+1]+"\n";
     i++;
   }
else{
    s=s+temp[0]+","+temp[1]+","+temp[2]+"\n";
   }
}
 System.out.println(s);

Comments

-1

I tried this:

String s="a,b,c\n";

while(s != null){ <<<<<-------Use the while condition as per your use case

    String[] var = s.split("\n");

    System.out.println(var[0]);
}

and it shows the desired result. You can split all the lines of the file similarly and get the desired output.

Output: a,b,c

Make sure to iterate the loop properly like buffer.readLine()

I hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.