Do not want to ignore new line character from file.

Reading lines ignores the last blank line!

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 7817 Views - Last Post: 11 February 2010 - 02:37 AM Rate Topic: -----

#1 godmoktail  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:01 PM

Hi, I'm doing a simple encoder program, using Scanner. I can read the file easily and encode it. Nevertheless, if the file has a single blank line (which supposedly is still considered as a line, just with a "\n" terminating character) at the end of the file, it will not consider it as a line.
For example, consider the text file:
Line#
1.    123
2.    ABC nop
3.     



In the while loop, I have count (line count) being incremented, but it will not if the last line of the file is blank. Here is the read portion of my code.

public String encode(File inputFile)throws FileNotFoundException
    {
        Scanner reader = new Scanner(inputFile);
        String line = "";

        int count = 0;
        while(reader.hasNextLine())
        {
            count++;
            line += reader.nextLine();
            // if there is a next line, add in the new line character that was skipped
            if(reader.hasNextLine())
            {
                line += "\n";
            }
        }

        System.out.println("This file has "+count+" lines.\n");
        .
        .
        .
}




Thanks in advance.
PS: I'm ashamed I haven't figured this out yet, and I consider myself an intermediate programmer. :(

Is This A Good Question/Topic? 0
  • +

Replies To: Do not want to ignore new line character from file.

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:14 PM

nextLine() reads past all the new lines, not just one. so "This is a string\n\n\n\n" is the same as "This is a string\n" when nextLine() is used on it.

You could use something like BufferedReader, and read the file one character at a time, and increment your counter if the character is a newline

This post has been edited by erik.price: 10 February 2010 - 01:36 PM

Was This Post Helpful? 2
  • +
  • -

#3 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,641
  • Joined: 20-September 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:21 PM

Your example file should report 3 lines - it does for me
Was This Post Helpful? 0
  • +
  • -

#4 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:23 PM

On my system this works fine as linux only uses "\n" as a new line character in text files.

If you are on windows and you're experiencing this problem it is probably because windows uses a new line character "\n" and a carriage return character "\r" to start a new line.

I don't know this for a fact but i would guess that Scanner.hasNextLine() is looking for "\n" not "\r". Try using a BufferedReader instead.
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,641
  • Joined: 20-September 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:24 PM

... and your if statement in the loop is redundant
Was This Post Helpful? 0
  • +
  • -

#6 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:27 PM

View Postgl3thr0, on 10 February 2010 - 02:23 PM, said:

On my system this works fine as linux only uses "\n" as a new line character in text files.

If you are on windows and you're experiencing this problem it is probably because windows uses a new line character "\n" and a carriage return character "\r" to start a new line.

I don't know this for a fact but i would guess that Scanner.hasNextLine() is looking for "\n" not "\r". Try using a BufferedReader instead.


I believe nextLine() works with any line terminators, so as to remain portable across platforms
Was This Post Helpful? 0
  • +
  • -

#7 godmoktail  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:31 PM

Yea, I programmed this earlier in the week and it worked fine. Gonna retry it on Ubuntu see what happens.

erik.price: Actually, it only reads up to one "\n". I added a ton of blank new lines, and it outputs the new lines, but not the final one.
Was This Post Helpful? 0
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:34 PM

View Postgodmoktail, on 10 February 2010 - 02:31 PM, said:

erik.price: Actually, it only reads up to one "\n". I added a ton of blank new lines, and it outputs the new lines, but not the final one.

Huh. My mistake then. Sorry about that :)
Was This Post Helpful? 1
  • +
  • -

#9 godmoktail  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:36 PM

Definitely works in Ubuntu. Gonna try with BufferedReader instead.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,641
  • Joined: 20-September 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:38 PM

BufferedReader shouldn't make any difference
Was This Post Helpful? 0
  • +
  • -

#11 godmoktail  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 01:54 PM

I tried with BufferedReader's read, which returns an int. Tried to convert it to string with Integer.toString(), but all I get is numbers.

Hmm, I don't think the BufferedReader's read returns an ascii value.
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,641
  • Joined: 20-September 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 02:02 PM

you need to use readLine
Was This Post Helpful? 0
  • +
  • -

#13 godmoktail  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 02:04 PM

My goal now is to convert the int that is returned from BufferedReader to a char (which hopefully is the char that is in the file being read and returned as an int). Anyone know any info on exactly what the int represents from the BufferedReader? I doubt it's an ascii value, although, that would be awesome since it will make testing for specific chars much easier.

Thanks in advance

Again, readLine ignores the newline character! I want the newline character, in order to get a complete file read and encode it. I want to encode every character, including whitespace and end of line feeds.
Was This Post Helpful? 0
  • +
  • -

#14 godmoktail  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 76
  • Joined: 10-May 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 02:14 PM

This managed to read everything just right, as it is. New line characters and all.
br is a BufferedReader initialized with a FileReader.
ch is an int
line is a String
Just reading the file, getting an int, converting the ascii to character, then that character to a string.
        while( (ch = br.read()) != -1)
        {
            char chc = (char)ch;
            String chs = Character.toString(chc);
            line += chs;
        }


This post has been edited by godmoktail: 10 February 2010 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Do not want to ignore new line character from file.

Posted 10 February 2010 - 03:30 PM

just check if the String returned by nextLine().trim().length() is not equal to 0
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2