Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

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

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    Member
    Join Date
    Feb 2010
    Location
    Auburn, AL
    Posts
    31
    Thanks
    0
    Thanked 8 Times in 8 Posts

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

    How are you reading the file? If you're reading it one line at a time, I'd imagine there's a chance that these characters are being stripped automatically. Ditto if you're using the Scanner to get words individually...

    If you're trying to read the file one character at a time, I'd imagine what you are doing would work. Do you have any code snippets to share?
    Let me know what you think of my website:
    http://www.auburn.edu/~carpept
    Comments or suggestions are appreciated!

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    I use readLine() from BufferedReader.

    I don't have the code anymore since I used another way, which is the split method from String. It stores the words in an array, and I just pick the last element in the array, since what I want is the last word of the line.

    But I still don't know how to stop if you read one char at a time (this is what I was doing)...

    Any suggestion ?

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

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

    Post your code for how you're reading in the file. It is possible that your method is dropping all EOL strings as many of Java's default readers use EOL to denote a point to stop reading. It is also possible that the data in your .txt file was stored using a "different" standard than the "\r\n" windows uses. In this case, it is probably just a '\n' character.

    There is also the probability that there is no EOL character sequence at the end of the file, in which case your program is trying to access data that simply isn't there resulting in the the StringIndexOutOfBoundsException. To prevent this, check to make sure that your not using charAt() outside of the string's length:

    if (index < str.length())
    {
         // do your charAt() comparison in here
    }

Similar Threads

  1. Java program to read last line of a file
    By JavaPF in forum File Input/Output Tutorials
    Replies: 2
    Last Post: September 10th, 2009, 02:26 AM
  2. How to detect audio input from a telephone call?
    By ces_31 in forum Java Theory & Questions
    Replies: 0
    Last Post: August 12th, 2009, 12:45 AM
  3. on-line shopping
    By sriraj.kundan in forum Web Frameworks
    Replies: 13
    Last Post: August 1st, 2009, 02:03 PM
  4. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM