THINK FIRST·CODE LATER

← All labs

File name inspector

Problem

FileName reads one line containing a file name (it may contain spaces and always contains a dot) and prints facts about it, using only String methods — no if:

length, lastIndexOf, substring, toLowerCase, contains, equals, startsWith, trim, replace.

The tidy name is the base name without spaces around it, in lower case, with every space replaced by _, followed by the extension in lower case. For the input

Lab Report final.PDF

the program prints

Length: 20
Position of the last dot: 16
Base name: Lab Report final
Extension: pdf
Contains a space: true
Is a PDF: true
Starts with Lab: true
Tidy name: lab_report_final.pdf

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.

l5-7-data-1.txt

Lab Report final.PDF
What your program must print for l5-7-data-1.txt
Length: 20
Position of the last dot: 16
Base name: Lab Report final
Extension: pdf
Contains a space: true
Is a PDF: true
Starts with Lab: true
Tidy name: lab_report_final.pdf

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 < l5-7-data-1.txt

Windows PowerShell does not understand <:

Get-Content l5-7-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.