CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2012
    Posts
    4

    Nextline delimiter in String.split

    Hello, I'm new to the forums. I'm learning Java and would appreciate help with a small issue. I'm trying to do the following: Enter a piece of text (all through console, in this case I'm working with Netbeans), and convert each line of the text into a String.

    String[] textArray = text.split("\r?\n|\r");

    The delimiter "\r?\n|\r" is one of the many I found in google, I have no idea if it's right. It seems to get the first line right and save it in textArray[0], but the array only has that one element regardless of the number of lines I enter.
    The console seems to allow the "nextline" character as input, so I think I have the wrong delimiter. Does anybody know which is the correct one?

    Any help will be greatly appreciated. Thanks in advance.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Nextline delimiter in String.split

    That doesn't convert a line of text into a string, it splits a string at the new line boundaries and returns an array of the parts. Can you explain exactly what you are trying to achieve and show all your code (remembering to use code tags).
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Aug 2012
    Posts
    4

    Re: Nextline delimiter in String.split

    Thanks for replying.
    I'll try to explain better. What I want to do is to convert a single String variable into an Array of Strings, so that each line of the String is an element of the String Array.
    This is an example of what I am trying to do:

    Code:
    Scanner in=new Scanner(System.in);
    System.out.println("Paste the text to divide into lines")
    String text=in.nextLine();
    String[] textArray = text.split("\r?\n|\r");
    System.out.println("The first line of the text is: "+textArray[0]);
    System.out.println("The second line of the text is: "+textArray[1]);
    // And so on...
    So, say that in the console I paste the following text:

    "This is a test.
    To see if it works."

    The output should be:

    The first line of the text is: This is a test.
    The second line of the text is: To see if it works.

    However, with the current delimiter ("\r?\n|\r") I have, it doesn't work, and that's what I'm looking for.

  4. #4
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Nextline delimiter in String.split

    Have you read the API docs on Scanner.nextLine() before using it? It usually helps to know how the methods you try to use work. If you had read the API docs you would have seen that nextLine "returns the rest of the current line, excluding any line separator at the end". Do you really need to use Scanner or was it just for a test?

  5. #5
    Join Date
    Aug 2012
    Posts
    4

    Re: Nextline delimiter in String.split

    Sorry, I'm really new to this and this is the only method for reading user input that I've learned this far. So, the problem here is Scanner and not String.split, right? There is no particular need to use Scanner, so is there another input method that would allow me to work with texts with multiple lines (i.e paste it in console and saving it as a String)?

  6. #6
    Join Date
    May 2009
    Location
    Lincs, UK
    Posts
    298

    Re: Nextline delimiter in String.split

    You can use Scanner for that, but then you don't have to use String.split(). Instead of reading the whole input as one string and splitting it, just read and store the lines one by one. In this case you can also use a Collection type (List/ArrayList) instead of the array, since you won't know in advance how many strings you will have.

  7. #7
    Join Date
    Aug 2012
    Posts
    4

    Re: Nextline delimiter in String.split

    I see. Copying and pasting each line to the console would not be practical for a long text, so I will try a different approach and work on a text file. That way I can read and save each line without so much trouble.
    Thanks for the help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured