Advertisement
Guest User

lab02

a guest
Jan 20th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. package down0310_l02;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.util.Scanner;
  6.  
  7. public class Lab02 {
  8.  
  9. public static void main(String[] args) {
  10.  
  11. try {
  12. File file = new File("pelee.txt");
  13. Scanner fileScanner = new Scanner(file);
  14. Scanner lineScanner = null;
  15. Scanner wordScanner = null;
  16. String line = "";
  17. String word;
  18. String clean_word;
  19. int line_count = 0;
  20. int word_count = 0;
  21. int number_count = 0;
  22.  
  23. while (fileScanner.hasNextLine()) {
  24. line = fileScanner.nextLine();
  25. lineScanner = new Scanner(line);
  26.  
  27. while(lineScanner.hasNext()){
  28.  
  29. if (lineScanner.hasNextInt()) {
  30. word = lineScanner.next();
  31. clean_word = "";
  32. for (int i = 0; i < word.length();i++){
  33. if (word.charAt(i)!=','){
  34. clean_word += word.charAt(i);
  35. }
  36. }
  37. Integer num = Integer.valueOf(clean_word);
  38. System.out.println("-----------------");
  39. System.out.println(num);
  40. System.out.println("-----------------");
  41. number_count++;
  42. }
  43. else {
  44. word = lineScanner.next();
  45. }
  46.  
  47. word_count++;
  48.  
  49. }
  50.  
  51. lineScanner.close();
  52.  
  53. line_count++;
  54. }
  55. System.out.println("Line Count: " + line_count);
  56. System.out.println("Word Count: " + word_count);
  57. System.out.println("Average Number of Words per Line: " + word_count/line_count );
  58. System.out.println("Number Count: " + number_count);
  59. fileScanner.close();
  60. // Print the results
  61.  
  62.  
  63.  
  64. } catch (FileNotFoundException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement