write a Java program that performs the following task on an input text file.
1. Determine the number of characters
2. Count the number new-lines
3. Identify the number of empty lines.

I have been working on this problem that I saw on the book and the part where I'm stuck on is identifying the number of empty lines. The text file is called "assignment.txt" and it contains the instructions above. This is my code so far:

import java.io.*;

public class Count
{
public void Lines()throws IOException
//counting the lines
{
File f = new File("assignment.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
br.readLine();

LineNumberReader ln = new LineNumberReader(br);
int count = 0;
while (ln.readLine()!=null)
{
count++;
}

System.out.println("No. of lines = " + count);

}
public void Charact()throws IOException
// counting characters
{
File f = new File("assignment.txt");
FileReader fr = new FileReader(f);
new BufferedReader(fr);
long numChar = f.length();
int countChar =0;
while(countChar<numChar)
{
countChar++;
}
System.out.println("No. of characters = " + countChar);

}

public static void main(String args[])throws IOException
{
Count ob1 = new Count();
ob1.Charact();
ob1.Lines();

}
}

br.readLine()

here you are reading a line of text, but you're not doing anything with it.

store the result of that method in a String object,

while ( result != null ){
// this will iterate as long as there are lines to read
}

you could have a lot of spaces, making the line itself looking to be 'not-empty', so, trim the result and compare that to an empty (space or "")

if equals add 1 to counter

as last line of the while loop,
overwrite your result-String with a new readLine

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.