4 Replies - 217 Views - Last Post: 01 January 2013 - 10:49 PM Rate Topic: -----

#1 yousuf.hussain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 12

Difference between the two code below

Posted 01 January 2013 - 10:31 PM

Hello Every one,

I am a confused betwwen the below 2 code.

Code 1:

while ((sCurrentLine = finput.readLine()) != null) {
System.out.println(sCurrentLine);
}

It display me the file content.

Code 2:

while ((finput.readLine()) != null) {
sCurrentLine=finput.readLine();
System.out.println(sCurrentLine);
}

Return me null value.

Could you please explain me the difference between this code.

Is This A Good Question/Topic? 0
  • +

Replies To: Difference between the two code below

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Difference between the two code below

Posted 01 January 2013 - 10:35 PM

The readLine() method returns a value. In the first snippet, each line is read, assigned to a temporary variable, then tested to see if it is null. If not, then the line is printed.

In the second snippet, the line is read. If it isn't null, the next line is read and printed.
Was This Post Helpful? 3
  • +
  • -

#3 yousuf.hussain  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-May 12

Re: Difference between the two code below

Posted 01 January 2013 - 10:45 PM

Thank for the quick response.

So u mean that if the file contain:

5
3
6
5

Then for the code 1 Output should be
5
3
6
5

And for the code 2 output should be
3
5

Correct me if I am wrong.

Thanks macosxnerd101.

I have tested it.

It work for me.
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: Difference between the two code below

Posted 01 January 2013 - 10:45 PM

That is it for even number lines, if you have an odd number of lines, you will end up with the null reading.... Because there is always unchecked read
Was This Post Helpful? 3
  • +
  • -

#5 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 822
  • View blog
  • Posts: 2,529
  • Joined: 29-July 11

Re: Difference between the two code below

Posted 01 January 2013 - 10:49 PM

Too late. :)

This post has been edited by farrell2k: 01 January 2013 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1