Solved programming problem

Count Total line,Character,Word from a Text file in Java.

Source code

package pkg1;

import java.io.BufferedReader;

import java.io.PrintWriter;

import java.io.IOException;

import java.io.FileReader;

import java.util.Scanner;

public class Main

{

    public static void main(String[] args)

    {

        BufferedReader br = null;

        String line;

        int total_char=0,total_word=0,total_line=0;

       // F:/Programing/Java/Book Solve/Java Programming - Bojlur/9.3/1/a.txt

        try

        {

            Scanner sc = new Scanner(System.in);

         

            System.out.println("Please Enter File Address: ");

            String address= sc.nextLine();

            br = new BufferedReader(new FileReader(address));      

                             

            while((line=br.readLine())!=null)

            {

               String a[] = line.split(" ");

                total_line++;

                total_word += a.length;

             

               for(int i=0;i<line.length();i++)

               {  

                   char c =line.charAt(i);

                   if(c!=' ')

                   {

                   total_char++;

                   }                

               }

            }                            

        }

        catch(IOException e)

        {

            e.printStackTrace();

            System.out.println("File Does Not Open");

        }

     

        System.out.println("Totla Char: "+total_char);

        System.out.println("Total Word: "+total_word);

        System.out.println("Total Line: "+total_line);

    }

}

Output

.txt File:

Please Enter File Address:

F:/Programing/Java/Book Solve/Java Programming - Bojlur/9.3/1/a.txt

Totla Char:33

Total Word:8

Total Line:3

Join the discussion

Add your comment