Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.io.*; class Count { public static void main(String args[]) throws IO

ID: 3656203 • Letter: I

Question

import java.io.*; class Count { public static void main(String args[]) throws IOException { int ch; boolean prev = true; //counters int Char_count = 0; int word_count = 0; int line_count = 0; // Attach FileInputStream fin = new FileInputStream(args[0]); //read the characters of the file till the end while ((ch = fin.read() ) != -1){ if (ch ! == " ") ++char_count; if (! prev && ch == " ") ++word_count; // dont count if previous char is space if (ch == " ") prev = true; else prev = false; if (ch == ' ') ++ line_count; } // display the count of characters, words, and lines char_count -= line_count * 2; word_count += line_count; System.out.println("No.of Chars=" + char_count); System.out.println("No. of words =" + word_count); System.out.println("No. of Lines=" + line_count); fin.close(); } }

Explanation / Answer

import java.io.*; class Count { public static void main(String args[]) throws IOException { int ch; String chValue; boolean prev = true; int char_count = 0; int word_count = 0; int line_count = 0; FileInputStream fin = new FileInputStream(args[0]); while ((ch = fin.read()) != -1) { chValue = Integer.toString(ch); if (chValue != " ") ++char_count; if (!prev && chValue == " ") ++word_count; if (chValue == " ") prev = true; else prev = false; if (ch == ' ') ++line_count; } char_count -= line_count * 2; word_count += line_count; System.out.println("No.of Chars=" + char_count); System.out.println("No. of words =" + word_count); System.out.println("No. of Lines=" + line_count); fin.close(); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote