Skip to content
ExamSpine

Lesson 1.3

Read a line and print it back

Part 1: Programs that talkReading, about 4 min2 exercises in this lesson

Java answers can't run on Spine yet. You can still read Java, trace it, and put an answer's lines in order.

Reading what the user types

By the end of this you will be able to write a program that reads a line the user types and prints it back.

So far your programs have printed the same thing every time. A program becomes useful when it answers what the user types. The simplest answer is to give the line straight back.

Reading one line

When your program reads a line, it stops and waits. The user types some text and presses Enter. Your program then has that line, as text, and carries on with the next instruction.

In Python, input() reads one line. In Java, a Scanner reads from the keyboard, and scanner.nextLine() reads one line. The page's Java starter already makes a Scanner called scanner for you, and its first line, import java.util.Scanner;, is what lets your program use one.

Here is a program that reads a line, prints it back, then prints one more line. In Java, only the import and main are shown: the class around main is the same as in your first program.

print(input())
print("That was your line.")
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println(scanner.nextLine());
    System.out.println("That was your line.");
}

The reading sits inside the brackets of the print. The computer does the inside first: it reads the line, then prints what it read. If the user types Good morning, the program prints:

Good morning
That was your line.

What the user types is not part of what your program prints. The checker types a line for your program and looks only at what your program prints back.

The line comes back exactly

The line is kept exactly as it was typed: capitals, punctuation, digits and any spaces at the start. 12 typed in is printed as 12. Nothing is changed unless your program changes it.

A common mistake: printing the example's text, such as "Good morning", instead of what was read. That matches one example and fails every other line. Your program is run with lines you have not seen, so print what was read, not a line you chose.

Now try it yourself on the questions below.

Exercise 1.3.11.3.1 · Write itMessage board

A lift has a small screen for messages. Write a program that reads one line typed by the user and prints it under a heading, on the line below.

  1. The first line is always Message:, with a capital M and a colon.
  2. The second line is the typed line, exactly as it was typed, including any spaces at the start. Spaces at the end of a line are ignored.
  3. Print nothing else.

Your program is run several times, with a different line typed each time. Only what it prints is checked.

In Python, read the line with input(), with nothing between the brackets. In Java, the page starts you with a class, its main method and a Scanner called scanner: read the line with scanner.nextLine().

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 4 times: the example below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • the heading, then the typed line (rules 1 and 2)

    You type

    1. typed: Back at 3pm

    It prints

    Message:
    Back at 3pm
solution.pySolution.java

Exercise 1.3.21.3.2 · Write itStar banner

Write a program that reads one line typed by the user and prints it as a banner, between two lines of stars.

  1. The first line is exactly five stars: *****.
  2. The second line is the typed line, exactly as it was typed, including any spaces at the start.
  3. The third line is five stars again, the same as the first.
  4. Print nothing else. Spaces at the end of a line are ignored.

Your program is run several times, with a different line typed each time. Only what it prints is checked.

In Python, read the line with input(), with nothing between the brackets. In Java, the page starts you with a class, its main method and a Scanner called scanner: read the line with scanner.nextLine().

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 4 times: the example below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • the typed line between two lines of stars (rules 1 to 3)

    You type

    1. typed: Grand opening

    It prints

    *****
    Grand opening
    *****
solution.pySolution.java