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

Write a short program in JAVA that reads a text file and counts the number of ch

ID: 3640494 • Letter: W

Question

Write a short program in JAVA that reads a text file and counts the number of characters at 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 output the number of words as well as the number of characters and number of blocks. The block size is 512 bytes. The program should change the first character of each line to the upper case character. It should eliminate multiple space between the words. (Use the ASCII value to make the conversion.) when the user specifies. The output of the program is in the following form:

Information about the file

Filename s line size l line size diff char# word# block#

Explanation / Answer

Please keep a input file in C drive or change the location to the current directory in the main method. 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 * @throws FileNotFoundException * @throws IOException */ public List processLineByLine(String fileName) throws FileNotFoundException, IOException { List resList = new ArrayList(); HashMap resMap = new HashMap(); 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 */ protected HashMap processLine(String aLine) { HashMap resMap = new HashMap(); List strList = new ArrayList(); StringTokenizer st = new StringTokenizer(aLine); while (st.hasMoreTokens()) { strList.add(st.nextToken()); } Iterator 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 resList, String fileName) { int totalCharCount = 0; int totalWordCount = 0; HashMap resMap = new HashMap(); List charList = new ArrayList(); List wordList = new ArrayList(); if (resList != null) { Iterator 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
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