THINK FIRST·CODE LATER

← All labs

Words report from text

Problem

Write a class WordReport that reads one line of text and prints a small report:

Words: 5
Longest: programming
Capitalised: Java Is A Fun Language
  • Words counts the words (separated by spaces);
  • Longest is the longest word (the first one, if several have the same length);
  • Capitalised prints the sentence with every word starting with a capital letter.

Use split(" ") and StringBuilder.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l17-2-data-1.txt

java is a fun language
What your program must print for l17-2-data-1.txt
Words: 5

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l17-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content l17-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.