Java Tutorial
🔍
Java ProgramsBasics & I/OSum of Two Numbers

Sum of Two Numbers in Java

beginner·  Basics & I/O  ·  I/O

Problem

Console input lets a program read values typed by the user while it's running, instead of relying only on hard-coded values.

Read two integers from the console and print their sum.

Input
4, 7
Output
Sum: 11

Java Program

Java
import java.util.Scanner; public class SumTwoNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); int a = sc.nextInt(); // reads and parses the typed number System.out.println(a); // echo back — this compiler doesn't echo typed input itself System.out.print("Enter second number: "); int b = sc.nextInt(); System.out.println(b); System.out.println("Sum: " + (a + b)); } }

Output

Enter first number: 4 Enter second number: 7 Sum: 11

Core Logic

Scanner handles the console input here — read two numbers and add them together.

How It Works
  1. 1new Scanner(System.in) creates a reader attached to the console.
  2. 2System.out.print(...) shows a prompt without a trailing newline, so the value read next appears to continue that same line.
  3. 3sc.nextInt() reads the next number typed and parses it directly into an int.
  4. 4System.out.println(a) immediately echoes the value back — most online compilers don't echo typed input themselves the way a real terminal does, so the program prints it explicitly.
  5. 5The same read-then-echo pair happens twice — once for each number — and the two ints are added and printed together.
Entering 4 then 7 prints Enter first number: 4, Enter second number: 7, and finally Sum: 11.
💡

Key Point: nextInt() only consumes the numeric token — it leaves the trailing newline in the buffer, which matters if you later mix in a nextLine() call.

Key Concepts

ScannernextInt()console input

Approach 2: BufferedReader

Java
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class SumTwoNumbersBufferedReader { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter first number: "); // readLine() returns a String, so parse it into an int explicitly int a = Integer.parseInt(br.readLine()); System.out.println(a); // echo back — this compiler doesn't echo typed input itself System.out.print("Enter second number: "); int b = Integer.parseInt(br.readLine()); System.out.println(b); System.out.println("Sum: " + (a + b)); } }

Output

Enter first number: 4 Enter second number: 7 Sum: 11

Core Logic

BufferedReader is a common alternative to Scanner — read each line as text, then parse it into a number yourself.

How It Works
  1. 1new BufferedReader(new InputStreamReader(System.in)) wraps standard input in a buffered character reader.
  2. 2br.readLine() reads one entire line of text at a time, returning it as a String.
  3. 3Integer.parseInt(...) converts that string into an int, since readLine() — unlike Scanner.nextInt() — has no built-in numeric parsing.
  4. 4System.out.println(a) echoes the parsed value back out, since online compilers typically don't echo typed input themselves the way a real terminal does.
  5. 5The method signature adds throws IOException, since readLine() can throw a checked exception on an I/O failure.
Typing 4 then 7 on separate lines parses to the ints 4 and 7, echoed back and followed by Sum: 11.
💡

Key Point: BufferedReader is noticeably faster than Scanner for reading large volumes of input, which is why it's the preferred choice in competitive programming — the trade-off is the extra manual parsing step.

Key Concepts

BufferedReaderreadLine()Integer.parseInt()

Related Programs