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

Write a static method name wordCountAndFixSpace that takes a scanner representin

ID: 3564510 • Letter: W

Question

Write a static method name wordCountAndFixSpace that takes a scanner representing a file as a parameter and that reports various statistics about the file. The method should generate the number of lines, the total number of words (regard "can't" as 1-word and disregard any marks or signs such as ?, ", ., - , , etc. when counting), the total number of characters, and the longest word. The text may contain multiple spaces and tabs. Those spaces should be reduced to single spaces between each word in order to print them to the console. Your program can assume that the text file may contain at least one line of input. The input file contains the following text: "Writing is an exercise." You get better and faster with practice. If you were going to run a marathon a year from now, would you wait for months and then run 26 miles cold? No, you would build up slowly, running most days. You might start on the flats and work up to more demanding and difficult terrain. To become a writer, write. Don't wait for that book manuscript or that monster external-review report to work on your writing.

Explanation / Answer

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;


public class FileFixer {

   public static void main(String[] args) throws IOException {

       Scanner file = new Scanner(new File("filetofix.txt"));
       wordCountAndFixSpace(file);
   }
  
  
   static void wordCountAndFixSpace(Scanner file){
      
       int noOfLines = 0;
       int noOfWords = 0;      
       while(file.hasNextLine()){
           String line = file.nextLine();
           noOfLines++;
          
           /*
           * Default delimiter for are " ": the space character,
           * the tab character, the newline character,
           * the carriage-return character, and the form-feed character.
           */
           StringTokenizer tokenizer = new StringTokenizer(line);
          
           while(tokenizer.hasMoreTokens()){
               tokenizer.nextToken();
               noOfWords++;
           }
       }
              
       System.out.println("No of Lines: "+noOfLines);
       System.out.println("No of Words: "+noOfWords);
      
   }

}

-----------------------------filetofix.txt---------------------------------

https://drive.google.com/file/d/0B1PCWH2ykvSBMGhrNy1JU2wwYlE/view?usp=sharing

------------------------------------output--------------------------------------

No of Lines: 9
No of Words: 80

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