1

Let's assume that I want to split the following string w.r.t \n

Hi!\nHow are u doing\nI am good

When I use the following code (i.e. initializing the string inside the code) :

String l = "Hi!\nHow are u doing\nI am good"
String[] sp = l.split("\\n");

It works as expected !

But if I get the input from InputStream :

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String l = br.readLine();    
String[] sp = l.split("\\n");

it doesn't split !

Any idea why this happens ?

15
  • Can you share the output that you are getting after using the split method Commented Dec 4, 2013 at 23:13
  • Try using l.split("\n"). Commented Dec 4, 2013 at 23:15
  • 1
    @ArianHosseinzadeh if you have \n in string literal like "a\nb" then \n means new line, but lets say that you read text from file that contains a\nb, then \n stops being one char, but is sequence of two characters '\' and 'n' so it is the same as "a\\nb" string. Your first example works because \\ in regex engine represents \ so regex is still looking for \n metacharacter. But if you change \n in your String l to \\n it will stop working because regex will not find new line \n but two characters '\' and 'n'. Commented Dec 4, 2013 at 23:32
  • 1
    @ArianHosseinzadeh then string that will be read is the same as "Hi!\\nHow are u doing\\nI am good". To make things work as you want you will need to replace "\\n" with "\n". You can do it with br.readLine().replace("\\n", "\n"); Commented Dec 4, 2013 at 23:37
  • 1
    @JeroenPeeters : I'm required to do this , maybe there's a problem with the program which is giving me the input, but this is what I have to solve. Commented Dec 4, 2013 at 23:41

3 Answers 3

6

You do not have anything to split in the second case as you are reading line. You read a line so you read until then \n there fore you can not split by it because it was split already by reader.

As stated in documentation

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

EDIT:

For your case what you are doing wrong is probably that you copy and paste the string from the code and expect to work in the same way.

The reason why does result whole input instead of output is that out can not type \n and expect to be equal to line feed. The reality is that you pass two char \ and n. So when you confirm it with return key value that is passed to your reading from System in is

[H,i,!,\,n,H,o,w, ,a,r,e, ,u, ,d,o,i,n,g,\,n,I, ,a,m, ,g,o,o,d]

where the firs string in your example is

[H,i,!,\n,H,o,w, ,a,r,e, ,u, ,d,o,i,n,g,\n,I, ,a,m, ,g,o,o,d]

To conclude string passed as input are treated in different manner than string defined in code. They may appear to be equal by are because \ has special meaning in Java.

Sign up to request clarification or add additional context in comments.

2 Comments

So what should I do ? it shouldn't be impossible
That depend on your goal. As you are reading from the System.in you always pass a line. There for you can not pass the Hi!\nHow are u doing\nI am good. You pass it line by line. Because when you type it and then confirm by return key. you string will be like Hi!\\nHow are u doing\\nI am good\n\r.
2

Problem is that when you are passing Hi!\nHow are u doing\nI am good as console input then \n is treated as two separate characters \ and n instead of one metacharacter \n. To make it back to \n you can just use replace("\\n", "\n") on data you get from console.

Comments

1

This is what you should do to read all lines from the inputstream:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String l = null;
while( (l = br.readLine()) != null) {
   System.out.println(l);
}   

4 Comments

This most definitely is working! Do you really pass newLine characters to the system.in? If you simply paste the string "Hi!\nHow are u doing\nI am good" in the console then IT WILL NOT WORK! You are not pasting new-lines, but actually the two characters '\' and 'n'. Really hit enter in the console, and it should work
I know ! but I am not passing the new-line character, I am passing the string you mentioned to the console and I don't hit the enter
But in java source code \n has special meaning. It is an escape character that is replaced with a newline. If you past "\n" in the console, then you are actually pasting the character sequence \n which is different. Your string l = "Hi!\nHow are u doing\nI am good" Does not contain the character sequence \n, but new lines. what exactly do you want? do you want to break on newlines? I guess so..
Actually I cannot change the input format because I receive it from another code which I don't have control on it, I have to solve it in my code

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.