Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

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.

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] + "\t");
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.

    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.

    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

Java, Programming

  • Category:- Java
  • Reference No.:- M9451293

Have any Question?


Related Questions in Java

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Assessment instructionsin this assessment you will complete

Assessment Instructions In this assessment, you will complete the programming of two Java class methods in a console application that registers students for courses in a term of study. The application is written using th ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Object-oriented software development1 introduction 11

OBJECT-ORIENTED SOFTWARE DEVELOPMENT 1. Introduction 1.1 Assignment Requirement 1.2 Deliverables and Structure (what to submit) 1.3 Software Restrictions 1.4 How to score high... 1.5 Assumptions 2. System Requirements 2. ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

In ruby the hash class inherits from enumerable suggesting

In Ruby, the Hash class inherits from Enumerable, suggesting to a programmer that Hashes are collections. In Java, however, the Map classes are not part of the JCF (Java Collections Framework). For each language, provide ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As