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

Write a short program that reads a text file and counts the number of characters

ID: 3729741 • Letter: W

Question

Write a short program that reads a text file and counts the number of characters in each line. Find the shortest line or record in the file and compare it with the longest line. Printout the differences in the number of characters between the shortest and longest lines. The program needs to out put the number of words as well as the number of characters and number of blocks. The block size is 512 bytes. The program shouid change the first character of each line to the upper case character. It should eliminate multiple space between words. (Use the ASCII value to meke the conversion.) when the user specifies. The output of the program is in the following form: Information about the file word # block # Filename s line size 1 line size diff char#

Explanation / Answer

package parser;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class FileParser {

private int blockSize = 516;
int charCount = 0;
int wordCount = 0;

/**
* This method is used to process the input file line by line.
*
* @param fileName
* @return List<HashMap<String, String>>
* @throws FileNotFoundException
* @throws IOException
*/
public List<HashMap<String, Integer>> processLineByLine(String fileName)
throws FileNotFoundException, IOException {
List<HashMap<String, Integer>> resList = new ArrayList<HashMap<String, Integer>>();
HashMap<String, Integer> resMap = new HashMap<String, Integer>();
try {
if (fileName != null) {
FileReader input = new FileReader(fileName);
BufferedReader bufRead = new BufferedReader(input);
String thisLine = null;
while ((thisLine = bufRead.readLine()) != null) {
resMap = processLine(thisLine);
resList.add(resMap);
}
bufRead.close();
} else {
System.out.println("File Name is null");
}
} catch (FileNotFoundException e1) {
// If file not found in current directory
throw e1;
} catch (IOException e) {
// If IO exception is generated, print a stack trace
throw e;
}
return resList;
}

/**
* This Method is used to parse the single line at a time and get the
* relevant fields
*
* @param aLine
* @return HashMap<String, String>
*/
protected HashMap<String, Integer> processLine(String aLine) {
HashMap<String, Integer> resMap = new HashMap<String, Integer>();
List<String> strList = new ArrayList<String>();
StringTokenizer st = new StringTokenizer(aLine);
while (st.hasMoreTokens()) {
strList.add(st.nextToken());
}
Iterator<String> it = strList.iterator();
while (it.hasNext()) {
charCount = charCount + it.next().length();
}
wordCount = strList.size();
resMap.put("charCount", charCount);
resMap.put("wordCount", wordCount);
return resMap;
}

/**
* This method is used to display the ailLine Details
*
* @param resList
*/
public void displayData(List<HashMap<String, Integer>> resList,
String fileName) {
int totalCharCount = 0;
int totalWordCount = 0;
HashMap<String, Integer> resMap = new HashMap<String, Integer>();
List<Integer> charList = new ArrayList<Integer>();
List<Integer> wordList = new ArrayList<Integer>();
if (resList != null) {
Iterator<HashMap<String, Integer>> it = resList.iterator();
while (it.hasNext()) {
resMap = it.next();
charCount = resMap.get("charCount");
wordCount = resMap.get("wordCount");
charList.add(charCount);
wordList.add(wordCount);
totalCharCount = totalCharCount + charCount;
totalWordCount = totalWordCount + wordCount;
}
} else {
System.out.println("List is empty");
}
Collections.sort(charList);
int shortLineSize = charList.get(0);
int longLineSize = charList.get(charList.size() - 1);
int diffSize = longLineSize - shortLineSize;
int blockCount = 0;
if(totalCharCount<blockSize){
blockCount = 1;
}
else{
blockCount = totalCharCount*4/512;
}
System.out.println(fileName + " short line size " + shortLineSize
+ " long line size " + longLineSize + " diff " + diffSize
+ " char# " + totalCharCount + " word# "+totalWordCount + " block# "+blockCount);
}

public static void main(String args[]) {
String fileName = "IC: putFile.txt";
FileParser fileParser = new FileParser();
List<HashMap<String, Integer>> hashMap;
try {
hashMap = fileParser.processLineByLine(fileName);
fileParser.displayData(hashMap, fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

*********************************************************************************************
Sample Input File

Hello how are you
i am doing good
how about you long time no see
sssssssss kkkk pppppp kkkkkkk hjhhhhhhhhhh iiiiiiiiii gt jjjjjjjjj gffffffffff iiiiiiiiii kk
Hello how are you
i am doing good
how about you long time no see
sssssssss kkkk pppppp kkkkkkk hjhhhhhhhhhh iiiiiiiiii gt jjjjjjjjj gffffffffff iiiiiiiiii kk
Hello how are you
i am doing good
how about you long time no see
sssssssss kkkk pppppp kkkkkkk hjhhhhhhhhhh iiiiiiiiii gt jjjjjjjjj gffffffffff iiiiiiiiii kk

******************************************************************************************************
sample output
InputFile.txt short line size 14 long line size 396 diff 382 char# 2250 word# 78 block# 17

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