Respuesta :
Answer:
Explanation:
The following code is written in Java. It is a function that takes the file name as a parameter. It then reads the file and counts the lines, words, and characters (excluding whitespace), saves these values in variables and then prints all of the variables to the console in an organized manner.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public static void countText(String fileName) throws FileNotFoundException {
int lines = 0;
int words = 0;
int characters = 0;
File myObj = new File(fileName);
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
lines += 1;
for (int x = 0; x < data.length(); x++) {
if (data.charAt(x) != ' ') {
characters += 1;
} else {
words += 1;
}
}
System.out.println(data);
}
System.out.println("File Contains:");
System.out.println(lines + " Lines");
System.out.println(words + " Words");
System.out.println(characters + " Characters");
myReader.close();
}

Following are Java Programs to count words, characters, and lines from the file.
Program to Count words into the file:
import java.io.*;//import package
import java.util.*;//import package
public class WordCount//defining the class WordCount
{
public static void main(String[] a) throws IOException //defining main method that uses the throws keyword for catch Exception
{
int wordCount=0,totalChar=0,lineCount = 0;//defining integer vaiable
String sCurrentLine;//defining String vaiable
File f = new File("words.txt");//creating the file class object
if(f.exists())//defining if block that checks file
{
Scanner sa = new Scanner(f);//creating Scanner class object that takes file as input
while (sa.hasNextLine())//defining loop that checks elements in file
{
sCurrentLine = sa.nextLine();//using String vaiable that holds input value
lineCount++;//incrementing the lineCount value
String words[] = sCurrentLine.split("\\s+");//defining String vaiable as array that holds split value of sCurrentLine vaiable
wordCount = wordCount + words.length;//ussing wordCount vaiable to hold length value
for(int i=0; i<words.length; i++)//defining loop that counts total length and store its value in totalChar
{
totalChar = totalChar + words[i].length();//holding length in totalChar vaiable
}
}
System.out.println("Total lines = "+lineCount);//printing the line values
System.out.println("Total words = "+wordCount);//printing the words values
System.out.println("Total chars = "+totalChar);//printing the Character values
}
}
}
Output:
Please find the attached file.
Program:
- import package
- Defining the class "WordCount."
- Inside the class defining the main method, we use the throws keyword to catch exceptions.
- Inside the method, three integer variables ("wordCount," "totalChar," and "lineCount"), and one string variable ("sCurrentLine") is declared.
- In the next line, File class and Scanner, a class object is created, which uses an if block that checks the file value and uses a Scanner class object that takes a file as input.
- In the next line, a while loop is declared that that counts and holds file value in line, character, and word form and uses another for loop that prints its values.
Find out more about the file handling here:
brainly.com/question/6845355

