Lesson 1.6
Read several lines, use them in any order
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.
Reading several lines and using them in any order
By the end of this you will be able to read several lines and use them in any order, not just the order they were typed.
In the last lesson you saved one line in a variable. When a program reads more than one line, each one goes into its own variable, and you can then use those variables in any order you like.
Reading two lines and using them backwards
Each input() (or scanner.nextLine() in Java) reads one line. The first call reads the first line typed; the second call reads the second. Save each one right away, then use the variables after both lines have been read.
first = input()
second = input()
print("You typed " + second
+ " and then " + first + ".")import java.util.Scanner;
public static void main(String[] args) {
Scanner scanner
= new Scanner(System.in);
String first = scanner.nextLine();
String second = scanner.nextLine();
System.out.println("You typed "
+ second + " and then "
+ first + ".");
}If the user types up on the first line and down on the second, the program prints:
You typed down and then up.
The variables are used in the opposite order from how they were read. That is the point: once a value is in a variable, you can reach it whenever you need it.
One variable per line
Give each line its own variable. If you save the first line, then read the second into the same name, the first line is gone (the last lesson showed why: a variable holds one value at a time). Two variables, one for each line, keeps both available.
A common mistake: reading a line but not saving it. input() on its own still reads and waits, but the line is thrown away. Always write name = input() (or String name = scanner.nextLine(); in Java) so the value is kept.
Now try it yourself on the questions below.
Exercise 1.6.11.6.1 · Write itSwap lines
Write a program that reads two lines typed by the user, one after the other, and prints them in the opposite order: the second line first, then the first line.
- The first line typed is printed second, and the second line typed is printed first.
- Each line is printed exactly as it was typed.
- Print nothing else. Spaces at the end of a line are ignored.
Your program is run several times, with different lines typed each time. Only what it prints is checked.
In Python, read each line with input() and save it in a variable. In Java, the page starts you with a class, its main method and a Scanner called scanner: read each line with scanner.nextLine() and save it in a String variable.
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 5 times: the 2 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.
two words, printed in the opposite order (rule 1)
You type
- typed: Hello
- typed: World
It prints
World Hello
another pair, swapped (rule 1)
You type
- typed: First
- typed: Second
It prints
Second First
Exercise 1.6.21.6.2 · Write itID card
Write a program that reads a name and a number, each on its own line, and prints an ID card on two lines:
Name: Ada ID: 4821
- The first line printed starts with
Name:and one space, then the name exactly as typed. - The second line printed starts with
ID:and one space, then the number exactly as typed. - The name is typed first, then the number.
- Print nothing else. Spaces at the end of a line are ignored.
Your program is run several times, with different names and numbers typed each time. Only what it prints is checked.
In Python, read each line with input() and save it in a variable. In Java, the page starts you with a class, its main method and a Scanner called scanner: read each line with scanner.nextLine() and save it in a String variable.
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.
a name and a number, labelled on two lines (rules 1 to 4)
You type
- typed: Ada
- typed: 4821
It prints
Name: Ada ID: 4821