Question

12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lin
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note : The first program is getting the filename entered by the user as input to the program from the console

the second program is passing inputfile(path of the file) through command line arguments.

Could you plz go through this code and let me know if u need any changes in this.Thank You

_______________________

1)

// aboutComputer.txt

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.

________________________

// CountLinesWordsChars.java

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class CountLinesWordsChars {
public static void main(String[] args) {
//Declaring variables
int count_lines=0;
int count_words=0;
int count_characters=0;

      
  
  
//Creating the Scanner class reference

       Scanner sc =null;
         
   /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc1 = new Scanner(System.in);
String line=null;
String fname;
String words[];
  
try{
   System.out.print("Enter the input filename :");
   fname=sc1.nextLine();
//Opening the file
sc= new Scanner(new File(fname));
  
//This loops continue to execute until the end of the file
while (sc.hasNext()){
  
//Counting the number of lines
count_lines++;
  
line=sc.nextLine();
  
//This for loop will count no of characters in the file
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)!=' ' && line.charAt(i)!='\n')
{
//Counting no of characters
count_characters++;
}
}
  
//splitting each line into String array
words=line.split(" ");
for(int j=0;j<words.length;j++)
{
  
//Counting no of words in the file
count_words++;
}
  
}
} catch (IOException e)
{
//Displaying the exception
System.out.println("Exception :"+e);
}
  
//Displaying the number of lines in the file
System.out.println("No of Lines in the File :"+count_lines);
  
//Displaying the number of words in the file
System.out.println("No of Words in the File :"+count_words);
  
//Displaying the number of characters in the file
System.out.println("No of Characters in the File :"+count_characters);
}
}
___________________________________

Output:

Enter the input filename :aboutComputer.txt
No of Lines in the File :8
No of Words in the File :75
No of Characters in the File :403

___________________________________

2)

inputfile1.txt (Save this file under D Drive.Then the path of the file pointing to it is D:\\inputfile1.txt)

A computer is a device that can be instructed to carry out an
arbitrary set of arithmetic or logical operations automatically.
The ability of computers to follow a sequence of operations
called a program make computers very flexible and useful.
Since ancient times simple manual devices like the abacus
aided people in doing calculations. Early in the Industrial
Revolution, some mechanical devices were built to automate
long tedious tasks, such as guiding patterns for looms.

__________________

NoOfLinesWordsChars.java

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class NoOfLinesWordsChars {

   public static void main(String[] args) {
//Declaring variables
int count_lines=0;
int count_words=0;
int count_characters=0;
  
  
//Assigning the file name to the String variable
String fname = args[0];
  
//Creating the Scanner class reference
Scanner sc=null;
String line=null;
String words[];
  
try{
//Opening the file
sc= new Scanner(new File(fname));
  
//This loops continue to execute until the end of the file
while (sc.hasNext()){
  
//Counting the number of lines
count_lines++;
  
line=sc.nextLine();
  
//This for loop will count no of characters in the file
for(int i=0;i<line.length();i++)
{
if(line.charAt(i)!=' ' && line.charAt(i)!='\n')
{
//Counting no of characters
count_characters++;
}
}
  
//splitting each line into String array
words=line.split(" ");
for(int j=0;j<words.length;j++)
{
  
//Counting no of words in the file
count_words++;
}
// count_words+=new StringTokenizer(line," ,").countTokens();
}
} catch (IOException e)
{
//Displaying the exception
System.out.println("Exception :"+e);
}
  
//Displaying the number of lines in the file
System.out.println("No of Lines in the File :"+count_lines);
  
//Displaying the number of words in the file
System.out.println("No of Words in the File :"+count_words);
  
//Displaying the number of characters in the file
System.out.println("No of Characters in the File :"+count_characters);

   }

}

_____________________

Output:

CWindows Command Processor E:丷javac NoOf LinesWordsChars-Java E: >java NoofLinesWordsChars D:inputfile1.txt No of Lines in th


_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
12.13 (Count characters, words, and lines in a file) Write a program that will count the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

    Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words). Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers: The count of lines/sentences The count...

  • Write a Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • In c programming, I need to write a program that reverses each of the lines in...

    In c programming, I need to write a program that reverses each of the lines in a file. The program accepts two command line arguments: The first one is the name of the input file The second is the name of the output file If the user didn't give two filenames, display an error message and exit. The program reads in each line of the input file and writes each line in reverse to the output file. Note, the lines...

  • Counting characters, words, and lines based on a text file

    I did the assigment but inside mainWrite a C program (called counting.c) that counts the number of characters, words and lines readfrom standard input (stdin) until EOF is reached. This means counting.c must contain a mainfunction along with other function.- Assume the input is ASCII text of any length.-Every byte read from stdin counts as a character except EOF.- Words are defined as contiguous sequences of letters (a through z, A through Z) and the apostrophe ( ' which has...

  • (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons...

    (1)Write a program in Python that reads an arbitrary-length file. All lines that start with semicolons (;) or pound signs (#) should be ignored. All empty lines must be ignored Lines containing a string between square brackets are sections. Within each section, lines may contain numbers or strings. For each section, calculate the lowest, highest and modal (most common) number, and print it. In each section, also count all non-whitespace characters, and print that. Example input: ; this is a...

  • Could I get some help with this assignment In this assignment, you'll write a program which...

    Could I get some help with this assignment In this assignment, you'll write a program which reads a text file, and prints a histogram of its word sizes. So, for example, the historgram should show the number of words of length 1, of length 2, etc., up to the number of words of length n, where n is some constant defined in your program. To obtain text files that are suitably long, you could try Project Gutenberg, a site containing...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT