Slip No 24 Q A

Q.Write a java program to count number of digits, spaces and characters from a file. [15 M]

import java.io.*;

class CountFileData
{
    public static void main(String args[]) throws IOException
    {
        FileReader fr = new FileReader("input.txt");

        int ch;
        int digits = 0, spaces = 0, chars = 0;

        // Read character by character
        while((ch = fr.read()) != -1)
        {
            char c = (char) ch;

            if(Character.isDigit(c))
                digits++;
            else if(Character.isWhitespace(c))
                spaces++;
            else if(Character.isLetter(c))
                chars++;
        }

        fr.close();

        System.out.println("Digits: " + digits);
        System.out.println("Spaces: " + spaces);
        System.out.println("Characters: " + chars);
    }
}
Spread the love

Leave a Comment

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

Scroll to Top