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

Write a method called wordLengths that accepts a Scanner representing an input f

ID: 3541901 • Letter: W

Question

Write a method called wordLengths that accepts a Scanner representing an input file as its argument. Your method should read from the given file, count the number of letters in each token in the file, and output a result diagram of how many words contain each number of letters. Use tabs before the asterisks so that they'll line up. If there are no words of a given length, omit that line from the output.


For example, if the file contains the following text:


Before sorting:

13 23 480 -18 75

hello how are you feeling today


After sorting:

-18 13 23 75 480

are feeling hello how today you

your method should produce the following output to the console:


2: 6    ******

3: 10   **********

5: 5    *****

6: 1    *

7: 2    **

8: 2    **

You may assume that no token in the file is more than 80 characters in length.

HOW DO I DO THIS?!?!?!?

Explanation / Answer

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package SCR;

/**
*
* @author ASHOK RAAVI
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;



public class PrintFileContent
{
   
void wordLength(Scanner sin)
{
    String result;
    int [] counts=new int[80];
while (sin.hasNextLine())
{
     result=sin.nextLine();
     String[] r = result.split(" ");
     for(int i=0;i<r.length;i++)
     {
         if(r[i].length()>0)
         counts[r[i].length()-1]=counts[r[i].length()-1]+1;
     }
}
for(int j=0;j<counts.length;j++)
{
      if(counts[j]>0)
      {
      System.out.print((j+1)+":"+counts[j]);
      for(int k=0;k<counts[j];k++)
          System.out.print(" *");
      System.out.println();
      }
}
}   
public static void main(String[] args) throws FileNotFoundException
{
PrintFileContent li=new PrintFileContent();
File file = new File("data.txt");
Scanner in = new Scanner(file);
li.wordLength(in);
}
}

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