Lesson 1.4
Join text with +
Part 1: Programs that talkReading, about 3 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.
Joining text with +
By the end of this you will be able to join pieces of text into one line, including a line the user typed.
So far, each print has printed one piece of text: a fixed line, or the line that was read. Often a line needs both, such as a greeting with the user's name in it. Joining text puts pieces together, one after another, into a single piece of text.
Joining with +
Between two pieces of text, + joins them: the second piece goes straight after the first. It adds nothing between them, not even a space. In Java, only main is shown below: the class around it is as before.
print("Good" + "morning")
print("Good" + " morning")public static void main(String[] args) {
System.out.println("Good" + "morning");
System.out.println("Good" + " morning");
}When it runs, the screen shows:
Goodmorning Good morning
The only difference is the space inside the quotes of the second line. If you want a space between two pieces, it has to be part of one of them.
Joining a typed line
The line your program reads is text too, so it can be joined in the same way. Here, fixed text goes on both sides of it:
print("You typed " + input() + ".")import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("You typed "
+ scanner.nextLine() + ".");
}If the user types the stairs, the program prints:
You typed the stairs.
The computer reads the line first, then joins the three pieces, then prints the one line that results. In Java, a long line can be split before a +, as above: it is still one instruction, ending at the semicolon.
A common mistake: forgetting the space next to the typed line. "You typed" + input() in Python, or "You typed" + scanner.nextLine() in Java, prints You typedthe stairs. Look at each place where two pieces meet and ask whether a space belongs there.
Now try it yourself on the questions below.
Exercise 1.4.11.4.1 · Write itName tag
Write a program that reads a name typed by the user and prints a name tag on one line: Tag: @ with the name straight after the @.
- The line starts with
Tag:: a capital T and a colon, then one space, then@. - The name comes straight after the
@, with no space between them, exactly as it was typed. - Everything is on one line. Print nothing else. Spaces at the end of a line are ignored.
Your program is run several times, with a different name typed each time. Only what it prints is checked.
In Python, read the name with input(). In Java, the page starts you with a class, its main method and a Scanner called scanner: read the name with scanner.nextLine(), build the whole line with +, and print it with one System.out.println.
You cannot use these in this question:
- .format()
- .join()
This question practises joining text with +, so build the whole line with + and print it once. The ways of building text listed here are not allowed.
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.
You cannot use these in this question:
- .print()
- .printf()
- .format()
- .join()
This question practises joining text with +, so build the whole line with + and print it once. The ways of building text listed here are not allowed.
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 tag, with the name straight after the @ (rules 1 to 3)
You type
- typed: ada_s
It prints
Tag: @ada_s
Exercise 1.4.21.4.2 · Write itClosed sign
Write a program that reads the name of a place typed by the user and prints a sign on one line: the name, then a colon straight after it, then one space and closed today.
- The line starts with the name, exactly as it was typed.
- The colon comes straight after the name, with no space before it.
- After the colon comes exactly one space, then
closed today., with a full stop at the end. - Everything is on one line. Print nothing else. Spaces at the end of a line are ignored.
Your program is run several times, with a different name typed each time. Only what it prints is checked.
In Python, read the name with input(). In Java, the page starts you with a class, its main method and a Scanner called scanner: read the name with scanner.nextLine(), build the whole line with +, and print it with one System.out.println.
You cannot use these in this question:
- .format()
- .join()
This question practises joining text with +, so build the whole line with + and print it once. The ways of building text listed here are not allowed.
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.
You cannot use these in this question:
- .print()
- .printf()
- .format()
- .join()
This question practises joining text with +, so build the whole line with + and print it once. The ways of building text listed here are not allowed.
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 name, the colon, then the rest of the sign (rules 1 to 4)
You type
- typed: The library
It prints
The library: closed today.