Lesson 6.7 · Write it
Welcome sign
A whole programPython or Java6 tests, 4 of them hidden
On a phone? Put the lines in order instead
Write a program that reads a guest's details from the file guest.txt and prints a welcome sign for them.
- The first line of
guest.txtis the guest's name. The second line is their table number. - Print
Welcome,followed by the name and!. Then printTable:followed by the table number. - A name can have spaces inside it, such as
Mei Lin, but never at its start or end. A table number is a whole number written with digits. - The file may have more lines after the second one. Ignore them. The last line may or may not end with a line end.
Close the file when you have read it: use with in Python, or try-with-resources in Java. The tests can only check what your program prints, not whether it closed the file, so checking that is up to you.
Your program is run several times, each time in a folder that holds only the files shown for that run.
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.
Examples
Your program is run 6 times: the 2 examples below, and 4 hidden ones that check the rules above. Only what it prints is checked. Each run starts in an empty folder that holds only the files shown for it.
a name, then a table number (rules 1 and 2)
You type
Nothing
It prints
Welcome, Ana! Table: 4
Files in its folder
guest.txt Ana 4
a name with a space inside it is printed whole (rule 3)
You type
Nothing
It prints
Welcome, Mei Lin! Table: 12
Files in its folder
guest.txt Mei Lin 12