i need help to count lines, paragraphs and articles ("a","an","the") in txt file with java. here is what i've got know:

import java.io.*;
import java.util.*;
public class Main
{
 public static void main ( String[] args ) throws FileNotFoundException, IOException 
 {
 
   String line=" ";
  char t=9; 
  String s=""+t; 
  int lines = 0;
  int para=0;
 Scanner scan =new Scanner (new File ("G:/data.txt"));
   
 while (scan.hasNext())
 {
    line = scan.nextLine();
    if (line.contains(s)) 
    {
       para++;
    }
    if (line!=null)
        
        
     lines++;
  
 }
   System.out.println("the nnumber of paragraphs is:"+para);
System.out.println("The number of lines is:"+lines);
   
 }
}

Recommended Answers

All 3 Replies

First of all, line will never be null so you don't need to check that. By doing this: while (scan.hasNext()) { ... You make sure that there is a next line in the file.

Also what java version are you using because the contains method was added at 1.5. I was about to tell that this was the mistake because I was looking the wrong API version.

So what is the value of the "s" variable.
Why don't you just do this: s = "a"; Also if you want the existence of "a" for example don't use contains because it will return false for any word that has the letter "a".
Use line.equalsIgnoreCase(s) And you need to post the errors that you are getting if any, and some sample input and output. Try a small file at first that you can check.

How to counte each letter

How to counte each letter

Check the String API. There is a method that lets you take each character from a word (String)

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.