Write a method called wordLengths that accepts a Scanner representing an input f
ID: 3557495 • 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:
your method should produce the following output to the console:
You may assume that no token in the file is more than 80 characters in length.
My Code:
public class WordLengths {
WordLengths() {}
public static void wordLengths(Scanner scan) {
int[] lengthCountArray = new int[100];
for(int i = 0; i < 100; ++i) {
lengthCountArray[i] = 0;
}
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] words = line.split("\s+");
for(int i = 0; i < words.length; ++i) {
int length = words[i].length();
++lengthCountArray[length];
}
}
for(int i = 1; i <= 80; ++i) {
if(lengthCountArray[i] > 0) {
System.out.print(i + ": " + lengthCountArray[i] + " ");
for(int j = 1; j <= lengthCountArray[i]; ++j) {
System.out.print("*");
}
System.out.println("");
}
}
}
public static (String[] args) throws FileNotFoundException {
// say input filename is "input.txt";
String filename = "input.txt";
Scanner scan = new Scanner(new File(filename));
//WordLengths.wordLengths(scan);
}
}
Errors:
Your code did not compile. Please read and correct the errors below.
Line 29
You have an illegal description of a data type here. Sometimes this happens when you misuse the syntax for generic types.
illegal start of type
public static (String[] args) throws FileNotFoundException { ^
Line 29
You may have forgotten to end a statement with a semicolon. Each Java statement must end with a semicolon.
';' expected
public static (String[] args) throws FileNotFoundException { ^
Line 29
You have an illegal description of a data type here. Sometimes this happens when you misuse the syntax for generic types.
illegal start of type
public static (String[] args) throws FileNotFoundException { ^
Line 29
You may have forgotten to type an identifier here, such as a variable's name or type.
<identifier> expected
public static (String[] args) throws FileNotFoundException { ^
Line 29
You may have forgotten to end a statement with a semicolon. Each Java statement must end with a semicolon.
';' expected
public static (String[] args) throws FileNotFoundException { ^
Line 29
You have an illegal description of a data type here. Sometimes this happens when you misuse the syntax for generic types.
illegal start of type
public static (String[] args) throws FileNotFoundException { ^
Line 29
You may have forgotten to type an identifier here, such as a variable's name or type.
<identifier> expected
public static (String[] args) throws FileNotFoundException { ^
Line 29
You may have forgotten to end a statement with a semicolon. Each Java statement must end with a semicolon.
';' expected
public static (String[] args) throws FileNotFoundException { ^ 8 errors
Explanation / Answer
import java.util.*;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
public class WordLengths {
WordLengths() {}
public static void wordLengths(Scanner scan) {
int[] lengthCountArray = new int[100];
for(int i = 0; i < 100; ++i) {
lengthCountArray[i] = 0;
}
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] words = line.split("\s+");
for(int i = 0; i < words.length; ++i) {
int length = words[i].length();
++lengthCountArray[length];
}
}
for(int i = 1; i <= 80; ++i) {
if(lengthCountArray[i] > 0) {
System.out.print(i + ": " + lengthCountArray[i] + " ");
for(int j = 1; j <= lengthCountArray[i]; ++j) {
System.out.print("*");
}
System.out.println("");
}
}
}
public static void main(String[] args) throws FileNotFoundException {
// say input filename is "input.txt";
String filename = "input.txt";
Scanner scan = new Scanner(new File(filename));
//WordLengths.wordLengths(scan);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.