Write a Java program that displays the number of characters, lines and words in a text

Java program that displays the number of characters line and word 

Counting the number of characters is important because almost all the text boxes that rely on user input have certain limit on the number of characters that can be inserted. For example, the character limit on a Facebook post is 63, 206 characters. Whereas, for a tweet on Twitter the character limit is 140 characters and the character limit is 80 per post for Snapchat.

Determining character limits become crucial when the tweet and Facebook post updates are being done through api’s. Java program that displays the number of characters line word

                                           Week – 4

Aim:

To Write a Java program that displays the number of characters, lines and words in a text.

Source Code:

import java.io.*;
class FILECH
{
           static int lines=0,words=0,chars=0;
           public static void wordCount(InputStreamReader isr)throws IOException
           {
                  int c=0;
                  while((c=isr.read())!=-1)
                  {
                  chars++;
                  if(c==’n’)                                                                                                         
                  lines++;
                  if((c==’n’)||(c==’ ‘)||(c==’t’))
                  ++words;
                  }
           }     
           public static void main(String rr[])throws IOException
           {
                  if(rr.length==0)
                  {
                                                                                                                                               System.out.println(“Enter the Text:”);
                  wordCount(new InputStreamReader(System.in));
                  }
                  else
                  {
                  FileReader fr=new FileReader(rr[0]);
                  wordCount(fr);
                  }
                  System.out.println(“Number of Words:”+words);
                  System.out.println(“Number of charecters:”+chars);                                              
                  System.out.println(“Number of lines: “+lines);
           }
}
 

Expected Output:

number of characters
Displays the number of characters
 
D:java>java FILECH
Enter the Text:
hai how r u ??
//////press Ctrl+z 
Number of Words:5
Number of charecters : 16
Number of lines: 1

sites: https://www.myprogrammingschool.com/

https://en.wikipedia.org/wiki/Characters_per_line

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment