Basics · 03
Reading input
Read text and numbers from the keyboard (standard input).
To solve problems you need to read input (standard input).
The example below reads a name on one line and a number on the next, then greets the name and prints twice the number.
name = input()n = int(input())print("Hi, " + name + "!")print(n * 2)const lines = require("fs").readFileSync(0, "utf8").split("\n");const name = lines[0];const n = Number(lines[1]);console.log("Hi, " + name + "!");console.log(n * 2);import { readFileSync } from "node:fs";const lines: string[] = readFileSync(0, "utf8").split("\n");const name: string = lines[0];const n: number = Number(lines[1]);console.log("Hi, " + name + "!");console.log(n * 2);import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String name = sc.nextLine(); int n = Integer.parseInt(sc.nextLine().trim()); System.out.println("Hi, " + name + "!"); System.out.println(n * 2); }}using System; class Program{ static void Main() { string name = Console.ReadLine(); int n = int.Parse(Console.ReadLine()); Console.WriteLine("Hi, " + name + "!"); Console.WriteLine(n * 2); }}#include <iostream>#include <string> int main() { std::string name; int n; std::getline(std::cin, name); std::cin >> n; std::cout << "Hi, " << name << "!" << '\n'; std::cout << n * 2 << '\n';}#include <stdio.h> int main(void) { char name[100]; int n; scanf("%99s", name); scanf("%d", &n); printf("Hi, %s!\n", name); printf("%d\n", n * 2); return 0;}name = Console.ReadLine();n = Console.ReadLine().ToNumber();Console.WriteLine("Hi, " + name + "!");Console.WriteLine(n * 2);Lara 3
Hi, Lara! 6
Key points
- Whatever you read is always a string.
To use it as a number, convert it. - You can read input a whole line at a time, or one space-separated token (a word or a number) at a time.
- For several numbers on one line, split the line at the spaces and convert each piece.
- When you don't know how many lines there are, read until the input ends.
Each language signals "the end" in its own way.
How each language writes it
| Python | A line is input(), convert with int(...).Several numbers on a line: map(int, input().split()).Until the end: for line in sys.stdin: |
|---|---|
| JavaScript | The usual way is to read everything at once with require("fs").readFileSync(0, "utf8") and split lines with split("\n").Convert with Number(...).For word-by-word reading, .trim().split(/\s+/) is safer |
| TypeScript | Same as JavaScript |
| Java | Read with Scanner's nextLine() and convert with Integer.parseInt(...).There is also nextInt() for a single number.Until the end: while (sc.hasNextInt()) |
| C# | Read with Console.ReadLine() and convert with int.Parse(...).Several numbers: Split(' ').Until the end: while ((line = Console.ReadLine()) != null) |
| C++ | std::cin >> n skips spaces and newlines and reads one token straight into a number.A whole line: std::getline(std::cin, s).Until the end: while (std::cin >> x) |
| C | scanf("%d", &n) — the & before the variable is required.%s reads only one word, up to a space; for a whole line use fgets.Until the end: while (scanf("%d", &x) == 1) |
| Craykon | A line is Console.ReadLine(), convert with .ToNumber().Several numbers on a line: Console.ReadNumbers() (gives a list of numbers).Until the end: until Console.ReadLine() returns null |
Common mistakes
Reading a line right after a numberJava · C++
int n = sc.nextInt();
String name = sc.nextLine();→ name is an empty string "", not the next line: it only read the newline left after the number
As in this example, read whole lines with nextLine() from the start and convert with Integer.parseInt.
C++ getline after cin >> n has the same problem.
Converting an empty line to a numberJavaScript · TypeScript
Number("")→ Silently 0, which hides the mistake of reading an empty line
A newline at the end of the input leaves one extra empty string at the end..trim() before splitting.
How languages differ
What you read is a string, so calculating without converting goes wrong.
In JavaScript, adding 1 to the input
What you read is a string, so calculating without converting goes wrong.
In JavaScript, adding 1 to the input
3 gives "3" + 1 → "31", and in Python input() + 1 is a TypeError.