Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- /* Write a java program that reads the contents of a text file then calculate and display
- 1. number of lines in the file
- 2. longest line in the file
- 3. shortest line int the file
- 4. average number of characters per line.
- */
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.Scanner;
- public class Lab2 {
- private static final String words = null;
- private static final String lines = null;
- static int previousLongLine = 0;
- static int previousShortLine = 10000;
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- System.out.println("File to be read: ");
- String fileName = console.next();
- String line = null;
- int key = 0, lineSize = 0,nbSentences = 0, lineNumber = 0;
- int word=0;
- Lab2 ln = new Lab2();
- HashMap longLineMap = new HashMap();
- HashMap shortLineMap = new HashMap();
- try {
- FileReader fileReader = new FileReader(fileName);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- while ((line = bufferedReader.readLine()) != null) {
- String []parts = line.split(" ");
- for( String w : parts)
- {
- nbSentences += getNbSentences(word);
- word++;
- }
- lineNumber++;
- lineSize = line.length();
- if (lineSize > previousLongLine) {
- previousLongLine = lineSize;
- longLineMap.clear();
- longLineMap.put(lineNumber, line);
- }
- if (lineSize < previousShortLine) {
- previousShortLine = lineSize;
- shortLineMap.clear();
- shortLineMap.put(lineNumber, line);
- }
- }
- System.out.println("Number of Words: "+ words);
- System.out.println("Number of Sentences: " + nbSentences);
- System.out.println("Number of lines: " + lines);
- //System.out.println("Average Words In Sentence: " + words / nbSentences);
- bufferedReader.close();
- } catch (FileNotFoundException ex) {
- System.out.println("Unable to open file '" + fileName + "'");
- } catch (IOException ex) {
- System.out.println("Error reading file '" + fileName + "'");
- }
- ln.printLongLine(longLineMap);
- ln.printShortLine(shortLineMap);
- }
- public static int getNbSentences(int word) {
- int result = 0;
- char[] chars = word.toCharArray();
- for(Character c : chars) {
- if(c == '.' || c == '!' || c == '?') {
- result++;
- }
- }
- return result;
- }
- public void printLongLine(HashMap longLineMap) {
- Set keyofSet = longLineMap.keySet();
- Iterator itr = keyofSet.iterator();
- while (itr.hasNext()) {
- Integer keys = (Integer) itr.next();
- String value = (String) longLineMap.get(keys);
- System.out.println("Line Number of Longest line: " + keys + "\nLongest line: " + value);
- }
- }
- public void printShortLine(HashMap shortLineMap) {
- Set keyofSet = shortLineMap.keySet();
- Iterator itr = keyofSet.iterator();
- while (itr.hasNext()) {
- Integer keys = (Integer) itr.next();
- String value = (String) shortLineMap.get(keys);
- System.out.println("Line Number of Shortest line: " + keys + "\nShortest line: " + value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement