Skip to content
ExamSpine

Lesson 1.8

Turn typed text into a number

Part 1: Programs that talkReading, about 4 min4 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.

Turning typed text into a number

By the end of this you will be able to turn typed text into a whole number or a decimal, compute with it, and print the result.

Everything the user types arrives as text. To do arithmetic with it, you convert it to a number first. In the last lesson you saw this briefly; now you will practise it.

Text to a whole number

age = int(input())
print("Next year: " + str(age + 1))
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    int age
        = Integer.parseInt(
            scanner.nextLine());
    System.out.println(
        "Next year: " + (age + 1));
}

If the user types 17, the program prints:

Next year: 18

In Python, int(input()) reads a line and turns it into a whole number in one step. In Java, Integer.parseInt does the conversion; the brackets around age + 1 make sure the addition happens before the joining.

Text to a decimal

For a number with a fraction, use float in Python or Double.parseDouble in Java.

price = float(input())
total = price * 1.1
print("With tax: " + str(total))
import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner
        = new Scanner(System.in);
    double price
        = Double.parseDouble(
            scanner.nextLine());
    double total = price * 1.1;
    System.out.println(
        "With tax: " + total);
}

If the user types 4.50, it prints With tax: 4.95. Python may print more decimal places for some inputs; round(value, 2) limits them but does not pad (so round(7.5, 2) gives 7.5, not 7.50). In Java, String.format("%.2f", value) pads to exactly two places (7.50).

What goes wrong

If the text is not the kind of number you asked for, the program stops with an error: ValueError in Python, NumberFormatException in Java. That includes three, and also 3.5 when you asked for a whole number with int() or Integer.parseInt. That is normal for now; later you will learn to check before converting.

A common mistake: forgetting to convert. In Python, input() gives text, so input() + input() joins two lines of text instead of adding two numbers. In Java, scanner.nextLine() gives a String, so adding two of them joins them in the same way. "3" + "4" is "34", not 7.

Now try it yourself on the questions below.

Exercise 1.8.11.8.1 · Write itAdd ages

Write a program that reads two ages, each a whole number on its own line, and prints their total on one line.

  1. The total is the two ages added together.
  2. Print only the total, as a number, with nothing else on the line. Spaces at the end are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use int to turn each typed line into a whole number before adding. In Java, use Integer.parseInt on each line.

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 ages added (rule 1)

    You type

    1. typed: 10
    2. typed: 7

    It prints

    17
  • one age is 0 (rule 1)

    You type

    1. typed: 0
    2. typed: 5

    It prints

    5
solution.pySolution.java

Exercise 1.8.21.8.2 · Write itDouble it

Write a program that reads a whole number and prints it doubled, with a label, on one line:

Double: 14
  1. The line starts with Double: and one space.
  2. After the space comes the number multiplied by 2.
  3. Print nothing else. Spaces at the end are ignored.
  4. The number is a whole number, which can be 0 or negative.

Your program is run several times. Only what it prints is checked.

In Python, use int to convert the typed line, compute the double, then join the label and the result with + (turn the number back into text with str). In Java, use Integer.parseInt. The multiplication happens before the joining even without brackets, but brackets around it make that easy to see.

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.

  • a number doubled with the label (rules 1 to 3)

    You type

    1. typed: 7

    It prints

    Double: 14
  • zero doubled is still zero (rules 2 and 4)

    You type

    1. typed: 0

    It prints

    Double: 0
solution.pySolution.java

Exercise 1.8.31.8.3 · Write itTotal price

Write a program that reads a price (a decimal number) on the first line and a quantity (a whole number) on the second line, and prints the total on one line.

  1. The total is the price multiplied by the quantity.
  2. Print only the total, as a number. Spaces at the end are ignored.

Your program is run several times. Only what it prints is checked.

In Python, use float for the price and int for the quantity. In Java, use Double.parseDouble for the price and Integer.parseInt for the quantity.

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.

  • a decimal price times a quantity (rule 1)

    You type

    1. typed: 2.50
    2. typed: 3

    It prints

    7.5
  • zero items (rules 1 and 2)

    You type

    1. typed: 5.00
    2. typed: 0

    It prints

    0.0
solution.pySolution.java

Exercise 1.8.41.8.4 · Write itType line

Write a program that reads a name (text) on the first line and a floor number (a whole number) on the second line, and prints one line:

Ada is on floor 3.
  1. The line starts with the name exactly as typed.
  2. Then is on floor , with one space before is and one after floor.
  3. Then the floor number, followed by a full stop with no space before it.
  4. Print nothing else. Spaces at the end of the line are ignored.

The floor number is always a whole number. In Python, input() gives you text, so you need int() to turn it into a number, and str() to turn a number back into text for joining with +. In Java, read the line as text and use Integer.parseInt to get an int; Java's + turns a number into text automatically when the other side is a String.

Your program is run several times. Only what it prints is checked.

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

  • a name and a floor number (rules 1 to 4)

    You type

    1. typed: Ada
    2. typed: 3

    It prints

    Ada is on floor 3.
solution.pySolution.java