Java Program How to Count Character Space Digit from File

Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java

Program 1

package dataflair;
import java.io.*;
public class TestCount 
{
    public static void main(String[] args) throws IOException
    {
       File F=new File("d://dataflair/student.txt");
       FileInputStream fis=null;
       char ch;
       int n;
       int cc=0,spc=0,dgc=0,sp=0;
       
       if(F.isFile())
       {
         fis=new FileInputStream(F);
         while((n=fis.read())!=-1)
         {
             ch=(char)(n);
             if(Character.isLetter(ch))
                cc++; 
             else
             if(Character.isSpaceChar(ch))
                 spc++;
             else
             if(Character.isDigit(ch))
                 dgc++;    
             else
                sp++; 
         }
           System.out.println("Total Character is: "+cc);
           System.out.println("Total Space is: "+spc);
           System.out.println("Total Digit is: "+dgc);
           System.out.println("Total Special Character is: "+dgc);
       }
       else
            System.out.println("\nFile not found.........");  
    }
    
}

 

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

courses

TechVidvan Team

TechVidvan Team provides high-quality content & courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.

Leave a Reply

Your email address will not be published. Required fields are marked *