Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4.  
  5.  
  6. /* Write a java program that reads the contents of a text file then calculate and display
  7. 1. number of lines in the file
  8. 2. longest line in the file
  9. 3. shortest line int the file
  10. 4. average number of characters per line.
  11. */
  12.  
  13.  
  14. import java.io.BufferedReader;
  15. import java.io.FileNotFoundException;
  16. import java.io.FileReader;
  17. import java.io.IOException;
  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import java.util.Set;
  21. import java.util.Scanner;
  22.  
  23. public class Lab2 {
  24.  
  25. private static final String words = null;
  26. private static final String lines = null;
  27. static int previousLongLine = 0;
  28. static int previousShortLine = 10000;
  29.  
  30. public static void main(String[] args) {
  31.  
  32. Scanner console = new Scanner(System.in);
  33. System.out.println("File to be read: ");
  34. String fileName = console.next();
  35.  
  36. String line = null;
  37. int key = 0, lineSize = 0,nbSentences = 0, lineNumber = 0;
  38. int word=0;
  39.  
  40. Lab2 ln = new Lab2();
  41. HashMap longLineMap = new HashMap();
  42. HashMap shortLineMap = new HashMap();
  43.  
  44. try {
  45. FileReader fileReader = new FileReader(fileName);
  46. BufferedReader bufferedReader = new BufferedReader(fileReader);
  47.  
  48. while ((line = bufferedReader.readLine()) != null) {
  49. String []parts = line.split(" ");
  50. for( String w : parts)
  51. {
  52. nbSentences += getNbSentences(word);
  53. word++;
  54. }
  55.  
  56. lineNumber++;
  57. lineSize = line.length();
  58. if (lineSize > previousLongLine) {
  59. previousLongLine = lineSize;
  60. longLineMap.clear();
  61. longLineMap.put(lineNumber, line);
  62. }
  63. if (lineSize < previousShortLine) {
  64. previousShortLine = lineSize;
  65. shortLineMap.clear();
  66. shortLineMap.put(lineNumber, line);
  67. }
  68.  
  69. }
  70. System.out.println("Number of Words: "+ words);
  71. System.out.println("Number of Sentences: " + nbSentences);
  72. System.out.println("Number of lines: " + lines);
  73. //System.out.println("Average Words In Sentence: " + words / nbSentences);
  74. bufferedReader.close();
  75.  
  76. } catch (FileNotFoundException ex) {
  77. System.out.println("Unable to open file '" + fileName + "'");
  78. } catch (IOException ex) {
  79. System.out.println("Error reading file '" + fileName + "'");
  80. }
  81.  
  82. ln.printLongLine(longLineMap);
  83. ln.printShortLine(shortLineMap);
  84. }
  85. public static int getNbSentences(int word) {
  86. int result = 0;
  87. char[] chars = word.toCharArray();
  88. for(Character c : chars) {
  89. if(c == '.' || c == '!' || c == '?') {
  90. result++;
  91. }
  92. }
  93. return result;
  94. }
  95. public void printLongLine(HashMap longLineMap) {
  96.  
  97. Set keyofSet = longLineMap.keySet();
  98. Iterator itr = keyofSet.iterator();
  99. while (itr.hasNext()) {
  100. Integer keys = (Integer) itr.next();
  101. String value = (String) longLineMap.get(keys);
  102. System.out.println("Line Number of Longest line: " + keys + "\nLongest line: " + value);
  103. }
  104. }
  105.  
  106. public void printShortLine(HashMap shortLineMap) {
  107.  
  108. Set keyofSet = shortLineMap.keySet();
  109. Iterator itr = keyofSet.iterator();
  110. while (itr.hasNext()) {
  111. Integer keys = (Integer) itr.next();
  112. String value = (String) shortLineMap.get(keys);
  113. System.out.println("Line Number of Shortest line: " + keys + "\nShortest line: " + value);
  114. }
  115.  
  116. }
  117.  
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement