1

I am taking a command line input string like this:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     String line = br.readLine();

I want to split this string which is:

String line = "int t; //variable t  
t->a = 0; //t->a does something  
return 0;"

like this:

String[] arr = line.split("\n");  
arr[0] = "int t; //variable t";  
arr[1] = "t->a=0; //t->a does something";  
arr[2] = "return 0";  

but when i run my java program that split function only returns this:

arr[0] = "int t; //variable t";

it didn't returns other two strings that i mentioned above,why this is happening please explain.

abhishek92
  • 111
  • 5
  • 1
    Are you sure there are `"\n"` in the `String`? This works fine for me. Print the string `line`, and tell us if the output is shown in 3 lines. – Christian Tapia Mar 24 '14 at 06:04
  • 3
    Just because you have made a line break in your code, it doesn't mean that there is a real line break in your string. – Marc Mar 24 '14 at 06:04
  • arr[0] contains the expected value. What does arr[1] contain? – Kon Mar 24 '14 at 06:04
  • Works for me: http://ideone.com/Jm3MZ7 Please double check your input –  Mar 24 '14 at 06:06
  • And beware for the future: String#split(String) takes a regex string, not a plain string. – damg Mar 24 '14 at 06:08
  • @Christian,@RC i am taking input from command line here is my code: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line = br.readLine(); when i print String line it gives only this int t; //variable t – abhishek92 Mar 24 '14 at 06:09
  • 1
    @abhishek92 `br.readLine()` takes the input line by line. of course you only got `"int t; //variable t"` because that was the only current value of `line`. you have nothing to split by `("\n")` – Baby Mar 24 '14 at 06:12
  • 2
    read line by line, join then split? something is wrong here –  Mar 24 '14 at 06:13
  • possible duplicate of [Split Java String by New Line](http://stackoverflow.com/questions/454908/split-java-string-by-new-line) – Sorter Mar 24 '14 at 06:13

3 Answers3

5

The method readLine() will read the input until a new-line character is entered. That new-line character is "\n". Therefore, it won't ever read the String separated by "\n".

One solution:

You can read the lines with a while loop and store them in an ArrayList:

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    List<String> lines = new ArrayList<String>();
    String line;

    while ((line = br.readLine()) != null) {
        lines.add(line);
    }

    for (String s : lines) {
        System.out.println(s);
    }

To stop the while you will have to press Ctrl + z (or Ctrl + d in UNIX, if I'm not wrong).

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

This should work!Also make sure if your input contains \n

 String[] arr = line.split("\r?\n")
Anirudha
  • 32,393
  • 7
  • 68
  • 89
0

From your comment you seem to take the input like this

   BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    try {
        String line = br.readLine();
        System.out.println(line);
    } catch (IOException e) {
        e.printStackTrace();
    } 

Here the line represents already split string by \n. When you hit enter on the console, the text entered in one line gets captured into line. As per me you are reading line, only once which captures just int t;//variable t, where there is nothing to split.

sanbhat
  • 17,522
  • 6
  • 48
  • 64