Results 1 to 6 of 6
  1. #1
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default How can I detect the end of a line in a file ?

    Hi folks,

    I would like to detect an end-of-line in my .txt file but can't find the right way to do.
    My program already detects the last word of the line so what I did is to look at each char of my last word using the "charAt()" method. More precisely, if I'm at position "i" of my word, I check whether the char(i+1) is equal to '\r' or '\n'.

    Before that, I did a System.getProperty("line.separator") and it returned "\r\n". So obviously, the char right after the last char of my word should be '\r', no ?

    When I'm positioned on the last char of my word and I try to do charAt(i+1), I get a "java.lang.StringIndexOutOfBoundsException".

    My question is : how come I can't detect this '\r' with charAt() ? Is there some way to detect it ?

    Thanks for your help!

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    How are you reading the file?

    If using a "readLine" type method, then the EOL is cut-off.

  3. #3
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Thanks for your quick reply masijade.

    Yes I'm using readLine() from BufferedReader.

    What I want is to save the last word of the String stored in readLine(), so I have to know when it starts (no problem for this) and when it ends (how?).

    I declared a StringBuffer. And I append each character of the last word. And this is made in a while loop. In words, my while loop does "while the next char is not a '\r', append the current char to the StringBuffer".

    Do you have any suggestion on how I can detect this end of line ? Or if you know how I can make simpler maybe ? My purpose is to save the last word of the line, that's all.

    Thanks in advance!

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    10

    Default

    use split (on whitespace) on the string you read using newline and simply take the last element of the array.

  5. #5
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Hi thanks for the tip

    My string has sometimes one space, but sometimes more. So I'm not sure about the regular expression I should write as argument of the String.split() method.

    If I put just " " as a delimiter, then, at the place where there are for instance two consecutives spaces in the String, the second space will be considered as a word (or token)...I tried the following RegEx but it doesn't work : ":?\\s"
    I get the same result.

    Any idea ?

  6. #6
    ze snow is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. Detect new file entry
    By rdemon in forum Advanced Java
    Replies: 4
    Last Post: 01-08-2010, 03:24 AM
  2. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  3. Replies: 7
    Last Post: 11-19-2008, 03:09 PM
  4. Replies: 0
    Last Post: 01-20-2008, 07:07 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

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