Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a Java Expert!

Join 414,938 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,680 people online right now.Registration is fast and FREE... Join Now!



BufferedReader Rate Topic: -----

#1 ksmith11  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-May 08


Dream Kudos: 0

Share |

BufferedReader

Post icon  Posted 20 May 2008 - 02:13 PM

When you have an input file and you want to use a BufferedReader to count the number of lines and return it, would you use


public static int countLines (BufferedReader in) throws IOExceptions
BufferedReader inputFile =new BufferedReader(new FileReader("in.txt"));
int lines = inputFile.readLine();




if so then what do you use after that to continue to the next line and so on?
Was This Post Helpful? 0
  • +
  • -


#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: BufferedReader

Posted 20 May 2008 - 03:00 PM

Typically you would setup a while loop and instead of setting int countlines to the line read, you would set countlines before the loop starts to 0 and for each read line you would increment the int count. Below is an example of how you would do this...

import java.io.*;

public class countlines {
	public static void main(String args[]) {
		// Setup your counter
		int countlines = 0;

		try {
			BufferedReader inputFile = new BufferedReader(new FileReader("c:\\test.txt"));
			String line = "";

			// For each line read, increment the counter
			while ((line = inputFile.readLine()) != null) {
				countlines++;
			}
		
			inputFile.close();


			// Print the number of lines
			System.out.println("Number of lines is: " + countlines);
		}
		catch (Exception e) {
			System.out.println("Error: " + e.getMessage().toString());
		}
	}
}



As you can see I setup a while loop that just reads each line using readline and increment the count. At the end I have the number of lines. You can of course adapt this to your function and return countlines.

Enjoy!

"At DIC we be line counting code ninjas... 1...2...3...6..4...9.. crap I lost count" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 ksmith11  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-May 08


Dream Kudos: 0

Re: BufferedReader

Posted 20 May 2008 - 03:38 PM

thanks man .. one more question.

what about if you want to read integers on several lines and then output the sum of them.
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • Icon

Reputation: 1434
  • View blog
  • Posts: 8,313
  • Joined: 18-April 07


Dream Kudos: 0

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

Re: BufferedReader

Posted 20 May 2008 - 03:41 PM

That is the beauty of this solution, each line is being read and stuck into the variable "line" in my example. Simply parse the line into an integer (using Integer.parseInt) and add it to a variable you have defined before the start of the loop. Once the loop is finished you have everything you need for the sum and even the average (this is given that each line is a single number).

sum variable / countlines = average

Now of course if each line has multiple integers, then you will need a loop inside the while loop to reach each number in the single line and sum them together.

The key is the while loop reading each line. It counts by incrementing countlines but also provides you access to the line read using the line variable.

:)
Was This Post Helpful? 0
  • +
  • -

#5 ksmith11  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-May 08


Dream Kudos: 0

Re: BufferedReader

Posted 20 May 2008 - 03:45 PM

alright thanks a bunch
Was This Post Helpful? 0
  • +
  • -

#6 ksmith11  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 18-May 08


Dream Kudos: 0

Re: BufferedReader

Posted 20 May 2008 - 03:54 PM

nevermind

This post has been edited by ksmith11: 20 May 2008 - 04:25 PM

Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users