JavaBook
Chapter 5· Java Foundations

Input & Output in Java

36 min read20 diagrams

Java Master Course — Chapter 5 of 50

In this chapter, we learn how a Java program communicates with the outside world: how it displays information on the screen and how it accepts information from the user.


1. What is Input and Output?#

Almost every useful program needs two basic abilities:

  1. Input — receive data from somewhere.
  2. Output — send/display data somewhere.

For example, imagine a program that calculates the area of a rectangle.

The program may need:

Output
Input:
Length = 10
Width  = 5

Processing:
Area = length × width

Output:
Area = 50

The basic flow is:

Diagram
          INPUT
            ↓
     ┌──────────────┐
     │ Java Program │
     │              │
     │ Processing   │
     └──────────────┘
            ↓
          OUTPUT

Input does not always have to come from a keyboard. It can come from:

  • keyboard
  • file
  • network
  • database
  • another program
  • command-line arguments
  • sensors or other devices

Similarly, output does not always mean the console. It can go to:

  • console
  • file
  • database
  • network
  • another program
  • GUI

In this chapter, our main focus is console input and output.


2. Console Input and Output#

When you run a normal Java program from a terminal, you commonly interact with three standard streams:

Output
System.in   → standard input
System.out  → standard output
System.err  → standard error

You can visualize them like this:

Diagram
Keyboard
   │
   ▼
System.in
   │
   ▼
Java Program
   │
   ├──────────────► System.out ─────► Normal output
   │
   └──────────────► System.err ─────► Error output

The System class provides these streams.


3. System.out#

The most common way to display output in Java is:

Java
System.out.println("Hello");

Let's understand each part.

System
  │
  └── out
       │
       └── println()

System is a class provided by Java.

out is a standard output stream.

println() prints something and then moves the cursor to the next line.


4. print() vs println()#

println()#

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello");
        System.out.println("Java");
    }
}

Output:

Output
Hello
Java

Each println() ends with a line break.


print()#

Java
public class Main {
    public static void main(String[] args) {
        System.out.print("Hello");
        System.out.print("Java");
    }
}

Output:

Output
HelloJava

print() does not automatically move to the next line.


Combining both#

Java
public class Main {
    public static void main(String[] args) {
        System.out.print("Hello ");
        System.out.println("Java");
        System.out.print("Welcome");
    }
}

Output:

Output
Hello Java
Welcome

5. Printing Variables#

You can print variables directly.

Java
public class Main {
    public static void main(String[] args) {
        int age = 20;
        double price = 99.50;
        String name = "Rahul";

        System.out.println(age);
        System.out.println(price);
        System.out.println(name);
    }
}

Output:

Output
20
99.5
Rahul

6. Printing Text and Variables Together#

Java allows string concatenation using +.

Java
public class Main {
    public static void main(String[] args) {
        String name = "Rahul";
        int age = 20;

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

Output:

Output
Name: Rahul
Age: 20

This is one of the most commonly used forms of output.


7. Important Rule: + with Strings#

When one side of + is a String, Java performs string concatenation.

Example:

Java
System.out.println("Age: " + 20);

Output:

Output
Age: 20

The number 20 is converted into text for concatenation.


7.1 Left-to-right evaluation#

This is important.

Consider:

Java
System.out.println(10 + 20 + " Java");

Evaluation happens from left to right:

10 + 20
   ↓
30 + " Java"
   ↓
"30 Java"

Output:

Output
30 Java

Now consider:

Java
System.out.println("Java " + 10 + 20);

First:

"Java " + 10
      ↓
"Java 10"

Then:

"Java 10" + 20
      ↓
"Java 1020"

Output:

Output
Java 1020

So:

Java
System.out.println(10 + 20 + " Java");

prints:

Output
30 Java

while:

Java
System.out.println("Java " + 10 + 20);

prints:

Output
Java 1020

If you want addition first, use parentheses:

Java
System.out.println("Java " + (10 + 20));

Output:

Output
Java 30

8. Printing Multiple Values#

You can concatenate many values.

Java
public class Main {
    public static void main(String[] args) {
        String name = "Aman";
        int age = 21;
        double marks = 87.5;

        System.out.println(
            "Name: " + name +
            ", Age: " + age +
            ", Marks: " + marks
        );
    }
}

Output:

Output
Name: Aman, Age: 21, Marks: 87.5

9. Escape Sequences#

Sometimes you need special characters inside strings.

Java provides escape sequences.

Common escape sequences:

Escape Meaning
\n New line
\t Tab
\" Double quote
\' Single quote
\\ Backslash
\b Backspace
\r Carriage return
\f Form feed

9.1 New line — \n#

Java
System.out.println("Hello\nJava");

Output:

Output
Hello
Java

9.2 Tab — \t#

Java
System.out.println("Name\tAge");
System.out.println("Aman\t20");

Typical output:

Output
Name    Age
Aman    20

9.3 Double quote — \"#

You cannot normally write an unescaped double quote inside a Java string.

This is invalid:

Java
System.out.println("He said "Hello"");

Use:

Java
System.out.println("He said \"Hello\"");

Output:

Output
He said "Hello"

9.4 Backslash — \\#

Java
System.out.println("C:\\Users\\Aman");

Output:

Output
C:\Users\Aman

This is especially important when working with Windows paths.


10. printf()#

Java also provides formatted output using:

Java
System.out.printf();

Example:

Java
public class Main {
    public static void main(String[] args) {
        String name = "Aman";
        int age = 20;

        System.out.printf("Name: %s, Age: %d%n", name, age);
    }
}

Output:

Output
Name: Aman, Age: 20

printf() is useful when you want precise control over formatting.


11. Common printf() Format Specifiers#

Specifier Used for
%d integer
%f floating-point number
%s String
%c character
%b boolean
%n platform-independent new line
%e scientific notation

Example:

Java
int age = 20;
double marks = 92.5678;
char grade = 'A';
boolean passed = true;
String name = "Aman";

System.out.printf(
    "Name=%s Age=%d Marks=%f Grade=%c Passed=%b%n",
    name, age, marks, grade, passed
);

Output:

Output
Name=Aman Age=20 Marks=92.567800 Grade=A Passed=true

12. Formatting Decimal Values#

Suppose you have:

Java
double price = 99.56789;

You may want only two digits after the decimal.

Use:

Java
System.out.printf("%.2f%n", price);

Output:

Output
99.57

Here:

Output
.2

means two digits after the decimal.


More examples#

Java
System.out.printf("%.1f%n", 12.345);
System.out.printf("%.2f%n", 12.345);
System.out.printf("%.3f%n", 12.345);

Output:

Output
12.3
12.35
12.345

The value is rounded for display.


13. Width and Alignment#

printf() can also control field width.

Example:

Java
System.out.printf("%10s%n", "Java");

The output reserves a field of width 10.

You can use this to make simple tables.

Java
System.out.printf("%-10s %5d%n", "Aman", 90);
System.out.printf("%-10s %5d%n", "Riya", 95);
System.out.printf("%-10s %5d%n", "Raj", 88);

Typical output:

Output
Aman          90
Riya          95
Raj           88

- means left alignment.

Without -, text/numbers are generally right aligned within the specified width.


14. System.out vs System.err#

Java provides:

Java
System.out

for normal output and:

Java
System.err

for error messages.

Example:

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Program started");

        System.err.println("Something went wrong");
    }
}

The exact appearance depends on the terminal/IDE, but conceptually:

Output
System.out → normal program output
System.err → error/diagnostic output

They are separate streams.


15. What is Input?#

Output is easy:

Output
Java → screen

Input is the reverse:

Output
Keyboard → Java

Java's standard input stream is:

Java
System.in

But directly reading raw bytes from System.in is not convenient for beginner-level input.

For console programs, a very common solution is:

Java
Scanner

16. Scanner#

Scanner is a class in:

Java
java.util

So you normally import it:

Java
import java.util.Scanner;

Basic example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Your age is: " + age);

        sc.close();
    }
}

Example interaction:

Output
Enter your age: 20
Your age is: 20

17. Understanding This Line#

Java
Scanner sc = new Scanner(System.in);

Break it into parts:

Scanner
   ↓
type/class

sc
   ↓
reference variable

new Scanner(...)
   ↓
creates a Scanner object

System.in
   ↓
source of input

This is also your first important example of an object being created from a class.

You will study this much more deeply in the OOP chapters.

For now, understand:

Java
Scanner sc

means sc is a reference capable of referring to a Scanner object.

And:

Java
new Scanner(System.in)

creates the object.


18. Reading an Integer#

Use:

Java
nextInt()

Example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        System.out.println("You entered: " + n);

        sc.close();
    }
}

Input:

Output
25

Output:

Output
Enter a number: 25
You entered: 25

19. Reading a long#

Use:

Java
nextLong()
Java
long population = sc.nextLong();

Example:

Java
System.out.print("Enter population: ");
long population = sc.nextLong();

System.out.println("Population = " + population);

20. Reading a double#

Use:

Java
nextDouble()
Java
double salary = sc.nextDouble();

Example:

Java
System.out.print("Enter salary: ");
double salary = sc.nextDouble();

System.out.println("Salary = " + salary);

21. Reading a float#

Use:

Java
nextFloat()
Java
float temperature = sc.nextFloat();

22. Reading a Boolean#

Use:

Java
nextBoolean()

Example:

Java
System.out.print("Are you a student? ");
boolean student = sc.nextBoolean();

System.out.println("Student: " + student);

Input:

Output
true

Output:

Output
Student: true

The input must be in a form that Scanner recognizes as a boolean, such as:

Output
true

or:

Output
false

23. Reading a Word with next()#

next() reads the next token.

Example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your name: ");
        String name = sc.next();

        System.out.println("Hello " + name);

        sc.close();
    }
}

Input:

Output
Aman

Output:

Output
Hello Aman

24. What is a Token?#

Suppose the user enters:

Output
Aman Sharma

There are two words/tokens:

Output
Aman
Sharma

If you call:

Java
sc.next()

you get only:

Output
Aman

The next next() would return:

Output
Sharma

25. Reading a Complete Line with nextLine()#

If you want to read the entire line, use:

Java
nextLine()

Example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your full name: ");
        String name = sc.nextLine();

        System.out.println("Hello " + name);

        sc.close();
    }
}

Input:

Output
Aman Sharma

Output:

Output
Hello Aman Sharma

26. next() vs nextLine()#

This difference is extremely important.

Suppose the user enters:

Output
Aman Sharma

Then:

Java
String x = sc.next();

stores:

Output
Aman

while:

Java
String x = sc.nextLine();

stores:

Output
Aman Sharma

So:

next()
   ↓
one token/word

nextLine()
   ↓
the complete remaining line

27. The Famous nextInt() + nextLine() Problem#

This is one of the most common beginner problems in Java.

Consider:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter age: ");
        int age = sc.nextInt();

        System.out.print("Enter name: ");
        String name = sc.nextLine();

        System.out.println("Age: " + age);
        System.out.println("Name: " + name);

        sc.close();
    }
}

Suppose input is:

Output
20
Aman Sharma

You may expect:

Output
Age: 20
Name: Aman Sharma

But name may become an empty string.

Why?


28. Why Does This Happen?#

Suppose the user types:

Output
20\n

The \n represents the Enter/newline.

nextInt() reads the integer:

Output
20

but it does not consume the line separator after it.

Conceptually:

Output
20\n
^^
nextInt() consumes 20

Remaining:
  \n

Then:

Java
sc.nextLine();

sees the remaining newline and consumes it.

So it returns an empty line.


29. Correct Solution#

After nextInt(), consume the remaining line:

Java
int age = sc.nextInt();
sc.nextLine();

String name = sc.nextLine();

Complete example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter age: ");
        int age = sc.nextInt();
        sc.nextLine();

        System.out.print("Enter full name: ");
        String name = sc.nextLine();

        System.out.println("Age: " + age);
        System.out.println("Name: " + name);

        sc.close();
    }
}

Input:

Output
20
Aman Sharma

Output:

Output
Age: 20
Name: Aman Sharma

Remember this pattern:

Java
nextInt();
nextLine();

when you need to switch from numeric token input to full-line input.


30. Another Approach: Read Everything as String#

For some applications, you can read input as strings and then convert it.

For example:

Java
String input = sc.nextLine();
int age = Integer.parseInt(input);

This approach can make line handling easier because you consistently read complete lines.

You will learn parsing in more detail below.


31. Scanner Input Methods#

Important methods:

Method Reads
next() next token
nextLine() complete line
nextInt() int
nextLong() long
nextShort() short
nextByte() byte
nextFloat() float
nextDouble() double
nextBoolean() boolean

There are also methods for checking whether the next input can be read safely.


32. Checking Input Before Reading#

Suppose you expect an integer.

You can use:

Java
hasNextInt()

Example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter an integer: ");

        if (sc.hasNextInt()) {
            int number = sc.nextInt();
            System.out.println("Number = " + number);
        } else {
            System.out.println("Invalid integer");
        }

        sc.close();
    }
}

Input:

Output
25

Output:

Output
Number = 25

If the user enters:

Output
hello

the program can report:

Output
Invalid integer

33. Other Useful hasNext...() Methods#

Examples:

Java
hasNextInt()
hasNextLong()
hasNextDouble()
hasNextBoolean()
hasNext()
hasNextLine()

These are useful for input validation.


34. Parsing Strings into Numbers#

Sometimes data arrives as a String.

For example:

Java
String ageText = "20";

But you need an int.

Use:

Java
int age = Integer.parseInt(ageText);

Example:

Java
public class Main {
    public static void main(String[] args) {
        String text = "25";

        int number = Integer.parseInt(text);

        System.out.println(number + 5);
    }
}

Output:

Output
30

Without parsing, text is a string:

Output
"25"

After parsing:

Output
25

is an integer.


35. Common Parsing Methods#

Method Result
Integer.parseInt() int
Long.parseLong() long
Double.parseDouble() double
Float.parseFloat() float
Short.parseShort() short
Byte.parseByte() byte
Boolean.parseBoolean() boolean

Example:

Java
int a = Integer.parseInt("100");
long b = Long.parseLong("100000");
double c = Double.parseDouble("12.5");
boolean d = Boolean.parseBoolean("true");

36. Invalid Parsing#

This is dangerous:

Java
int number = Integer.parseInt("hello");

Java cannot convert "hello" into an integer.

This results in a:

Output
NumberFormatException

You will study exceptions properly in Chapter 25.

For now, remember:

Output
String → valid numeric text → parsing works
String → invalid numeric text → exception

Examples:

Java
Integer.parseInt("123");   // works
Integer.parseInt("-50");   // works
Integer.parseInt("12.5");  // fails
Integer.parseInt("abc");   // fails

37. Building a Simple Calculator#

Now combine input, variables, operators, and output.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        double a = sc.nextDouble();

        System.out.print("Enter second number: ");
        double b = sc.nextDouble();

        double sum = a + b;
        double difference = a - b;
        double product = a * b;

        System.out.println("Sum = " + sum);
        System.out.println("Difference = " + difference);
        System.out.println("Product = " + product);

        if (b != 0) {
            System.out.println("Division = " + (a / b));
        } else {
            System.out.println("Cannot divide by zero");
        }

        sc.close();
    }
}

Example:

Output
Enter first number: 20
Enter second number: 5
Sum = 25.0
Difference = 15.0
Product = 100.0
Division = 4.0

This is an important milestone because you are now combining:

Input
  ↓
Variables
  ↓
Operators
  ↓
Conditions
  ↓
Output

38. Student Marks Program#

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter marks of subject 1: ");
        double m1 = sc.nextDouble();

        System.out.print("Enter marks of subject 2: ");
        double m2 = sc.nextDouble();

        System.out.print("Enter marks of subject 3: ");
        double m3 = sc.nextDouble();

        double total = m1 + m2 + m3;
        double percentage = total / 3;

        System.out.println("Total = " + total);
        System.out.println("Percentage = " + percentage);

        sc.close();
    }
}

Example:

Output
Enter marks of subject 1: 80
Enter marks of subject 2: 90
Enter marks of subject 3: 70
Total = 240.0
Percentage = 80.0

39. Reading a Character#

Scanner does not have a simple nextChar() method.

A common way is:

Java
char ch = sc.next().charAt(0);

Example:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = sc.next().charAt(0);

        System.out.println("You entered: " + ch);

        sc.close();
    }
}

Input:

Output
A

Output:

Output
You entered: A

40. How next().charAt(0) Works#

Suppose input is:

Output
Java

Then:

Java
sc.next()

returns:

Output
"Java"

Then:

Java
.charAt(0)

takes the character at index 0:

Output
J

Remember that String indexing starts at zero.

Output
Java
0123

Therefore:

Java
"Java".charAt(0)

gives:

Output
J

41. Complete User Registration Example#

Let's build a slightly larger program.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter your full name: ");
        String name = sc.nextLine();

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.print("Enter your percentage: ");
        double percentage = sc.nextDouble();

        System.out.print("Are you a student? ");
        boolean student = sc.nextBoolean();

        System.out.println();
        System.out.println("----- Profile -----");
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Percentage: " + percentage);
        System.out.println("Student: " + student);

        sc.close();
    }
}

Example input:

Output
Aman Sharma
20
88.5
true

Output:

Output
----- Profile -----
Name: Aman Sharma
Age: 20
Percentage: 88.5
Student: true

42. What Does sc.close() Do?#

You may see:

Java
sc.close();

This closes the Scanner.

Because this Scanner is reading from:

Java
System.in

closing the Scanner also closes the underlying input stream.

For a small program that is about to terminate, this is usually fine.

However, you should understand that after closing System.in, you cannot normally use that same standard input stream again in the same program.

For beginner console programs, this pattern is common:

Java
Scanner sc = new Scanner(System.in);

// use scanner

sc.close();

43. Should We Create Multiple Scanners for System.in?#

Usually, no.

Avoid doing this:

Java
Scanner sc1 = new Scanner(System.in);
Scanner sc2 = new Scanner(System.in);

Instead, create one Scanner and reuse it:

Java
Scanner sc = new Scanner(System.in);

Then use:

Java
sc.nextInt();
sc.nextLine();
sc.nextDouble();

as required.


44. BufferedReader#

Another important Java input class is:

Java
BufferedReader

It belongs to:

Java
java.io

Example:

Java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br =
            new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter your name: ");
        String name = br.readLine();

        System.out.println("Hello " + name);
    }
}

Input:

Output
Aman Sharma

Output:

Output
Hello Aman Sharma

45. Understanding BufferedReader#

This line looks complicated:

Java
BufferedReader br =
    new BufferedReader(new InputStreamReader(System.in));

Conceptually:

System.in
   ↓
InputStreamReader
   ↓
BufferedReader
   ↓
readLine()

Each layer has a role.

System.in:

Output
raw byte input

InputStreamReader:

Output
converts bytes into characters

BufferedReader:

Output
provides efficient character/line-oriented reading

For now, you do not need to memorize the internal implementation.


46. Scanner vs BufferedReader#

Both can read console input, but they work differently.

Feature Scanner BufferedReader
Package java.util java.io
Beginner friendly Very high Medium
Reads int directly Yes No
Reads double directly Yes No
Reads complete line Yes Yes
Parsing required Often no Often yes
Convenient token parsing Yes No
Typical use Beginner/simple input Line-oriented input/advanced use

Example with Scanner:

Java
int age = sc.nextInt();

With BufferedReader:

Java
int age = Integer.parseInt(br.readLine());

47. Why Does BufferedReader Need Parsing?#

readLine() returns a String.

Suppose the user enters:

Output
25

This:

Java
String text = br.readLine();

produces:

Output
"25"

not:

Output
25

To convert it:

Java
int age = Integer.parseInt(text);

Therefore:

Keyboard
   ↓
readLine()
   ↓
"25"
   ↓
Integer.parseInt()
   ↓
25

48. Scanner vs BufferedReader — Which Should You Use?#

For beginners and small console programs:

Java
Scanner

is usually easier.

For line-based input and some performance-sensitive situations:

Java
BufferedReader

can be preferable.

Do not fall into the trap of thinking:

Output
Scanner = bad
BufferedReader = always better

The correct tool depends on the problem.

If readability and convenience matter, Scanner is often perfectly suitable.


49. Command-Line Arguments#

Look at the main method:

Java
public static void main(String[] args)

You have already seen args.

args contains command-line arguments supplied when the program starts.

Example program:

Java
public class Main {
    public static void main(String[] args) {
        System.out.println(args[0]);
    }
}

Suppose you run:

Output
java Main Hello

Then:

Output
args[0] = "Hello"

Output:

Output
Hello

50. Multiple Command-Line Arguments#

Program:

Java
public class Main {
    public static void main(String[] args) {
        System.out.println("First: " + args[0]);
        System.out.println("Second: " + args[1]);
    }
}

Run:

Output
java Main Java Python

Output:

Output
First: Java
Second: Python

The command-line arguments are Strings.

If you need a number:

Java
int age = Integer.parseInt(args[0]);

Example:

Output
java Main 20

Then:

Java
int age = Integer.parseInt(args[0]);

produces:

Output
20

as an integer.


51. args and Scanner Are Different#

Do not confuse these two types of input.

Command-line arguments:

Program starts
     ↓
arguments already supplied

Scanner:

Program starts
     ↓
program waits for user input

Example command-line:

Output
java Main Aman 20

Example Scanner interaction:

Output
Enter name: Aman
Enter age: 20

Both are input, but they happen differently.


52. Checking args.length#

Never blindly access:

Java
args[0]

if you are not sure an argument was supplied.

You can check:

Java
if (args.length > 0) {
    System.out.println(args[0]);
}

Example:

Java
public class Main {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("No arguments supplied");
            return;
        }

        System.out.println("First argument: " + args[0]);
    }
}

If run without arguments:

Output
No arguments supplied

53. A Simple Command-Line Calculator#

Java
public class Main {
    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Please provide two numbers.");
            return;
        }

        double a = Double.parseDouble(args[0]);
        double b = Double.parseDouble(args[1]);

        System.out.println("Sum = " + (a + b));
    }
}

Run:

Output
java Main 10 20

Output:

Output
Sum = 30.0

Notice the flow:

args[0] → "10"
             ↓
Double.parseDouble()
             ↓
10.0

args[1] → "20"
             ↓
Double.parseDouble()
             ↓
20.0

54. Input Validation#

Real programs cannot assume that users always enter correct data.

Suppose your program asks:

Output
Enter age:

A user may enter:

Output
twenty

instead of:

Output
20

Your program should handle invalid input appropriately.

With Scanner:

Java
if (sc.hasNextInt()) {
    int age = sc.nextInt();
    System.out.println("Age = " + age);
} else {
    System.out.println("Invalid age");
}

This is an early introduction to validation.

Later, when you study exception handling, you will learn another approach.


55. Input Validation with a Loop#

You can repeatedly ask until the user enters a valid integer.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int age;

        while (true) {
            System.out.print("Enter your age: ");

            if (sc.hasNextInt()) {
                age = sc.nextInt();
                break;
            }

            System.out.println("Invalid input. Please enter a number.");
            sc.next();
        }

        System.out.println("Your age is: " + age);

        sc.close();
    }
}

The important part is:

Java
sc.next();

inside the invalid-input case.

It removes the invalid token so the loop can try again.


56. A Practical Login Example#

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter username: ");
        String username = sc.nextLine();

        System.out.print("Enter age: ");
        int age = sc.nextInt();

        System.out.println();
        System.out.println("----- User Details -----");
        System.out.println("Username: " + username);
        System.out.println("Age: " + age);

        sc.close();
    }
}

Example:

Output
Enter username: akshit
Enter age: 20

----- User Details -----
Username: akshit
Age: 20

This looks simple, but it contains several important Java concepts:

Class
↓
main()
↓
Scanner object
↓
Input
↓
Variables
↓
Output

57. Input → Processing → Output#

A very useful programming model is:

Diagram
          ┌──────────┐
          │  INPUT   │
          └────┬─────┘
               ↓
        ┌──────────────┐
        │  PROCESSING  │
        └──────┬───────┘
               ↓
          ┌──────────┐
          │  OUTPUT  │
          └──────────┘

Example: calculate circle area.

Input:

Output
radius = 5

Processing:

Output
area = π × radius × radius

Output:

Output
Area = 78.5398...

Code:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter radius: ");
        double radius = sc.nextDouble();

        double area = Math.PI * radius * radius;

        System.out.printf("Area = %.2f%n", area);

        sc.close();
    }
}

Input:

Output
5

Output:

Output
Area = 78.54

58. Practical Program: Temperature Conversion#

Formula:

Output
Celsius = (Fahrenheit - 32) × 5 / 9

Program:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter temperature in Fahrenheit: ");
        double fahrenheit = sc.nextDouble();

        double celsius = (fahrenheit - 32) * 5 / 9;

        System.out.printf("Celsius = %.2f%n", celsius);

        sc.close();
    }
}

Input:

Output
98.6

Output:

Output
Celsius = 37.00

59. Practical Program: Simple Interest#

Formula:

Output
SI = (P × R × T) / 100

where:

Output
P = Principal
R = Rate
T = Time

Code:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter principal: ");
        double p = sc.nextDouble();

        System.out.print("Enter rate: ");
        double r = sc.nextDouble();

        System.out.print("Enter time: ");
        double t = sc.nextDouble();

        double si = (p * r * t) / 100;

        System.out.println("Simple Interest = " + si);

        sc.close();
    }
}

60. Practical Program: BMI Calculator#

Formula:

Output
BMI = weight / (height × height)

where weight is in kilograms and height is in meters.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter weight in kg: ");
        double weight = sc.nextDouble();

        System.out.print("Enter height in meters: ");
        double height = sc.nextDouble();

        double bmi = weight / (height * height);

        System.out.printf("BMI = %.2f%n", bmi);

        sc.close();
    }
}

61. Practical Program: Average of Three Numbers#

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter first number: ");
        double a = sc.nextDouble();

        System.out.print("Enter second number: ");
        double b = sc.nextDouble();

        System.out.print("Enter third number: ");
        double c = sc.nextDouble();

        double average = (a + b + c) / 3;

        System.out.printf("Average = %.2f%n", average);

        sc.close();
    }
}

Input:

Output
10
20
30

Output:

Output
Average = 20.00

62. Practical Program: User Profile#

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter name: ");
        String name = sc.nextLine();

        System.out.print("Enter age: ");
        int age = sc.nextInt();

        System.out.print("Enter height in cm: ");
        double height = sc.nextDouble();

        System.out.print("Are you a student? ");
        boolean student = sc.nextBoolean();

        System.out.println();
        System.out.println("====================");
        System.out.println("      PROFILE");
        System.out.println("====================");

        System.out.println("Name    : " + name);
        System.out.println("Age     : " + age);
        System.out.println("Height  : " + height + " cm");
        System.out.println("Student : " + student);

        sc.close();
    }
}

63. Output Formatting for Tables#

Suppose you want to print student marks.

Java
System.out.printf("%-15s %10s%n", "Name", "Marks");
System.out.printf("%-15s %10.2f%n", "Aman", 88.50);
System.out.printf("%-15s %10.2f%n", "Riya", 91.25);
System.out.printf("%-15s %10.2f%n", "Raj", 76.75);

Output:

Output
Name                 Marks
Aman                 88.50
Riya                 91.25
Raj                  76.75

This becomes very useful when creating command-line applications.


64. println() vs printf()#

Use println() when you want simple output:

Java
System.out.println("Hello " + name);

Use printf() when formatting matters:

Java
System.out.printf("Hello %s, your score is %.2f%n", name, score);

Think:

Output
println → simple
printf  → formatted

65. print() vs println() vs printf()#

Method Purpose
print() print without automatic new line
println() print and move to next line
printf() formatted output

Example:

Java
System.out.print("A");
System.out.println("B");
System.out.printf("%d%n", 10);

Output:

Output
AB
10

66. Common Beginner Mistakes#

Mistake 1 — Forgetting the import#

Wrong:

Java
Scanner sc = new Scanner(System.in);

if you haven't imported Scanner.

Correct:

Java
import java.util.Scanner;

Mistake 2 — Using next() for a full name#

Java
String name = sc.next();

Input:

Output
Aman Sharma

Result:

Output
Aman

For the complete name:

Java
String name = sc.nextLine();

Mistake 3 — nextInt() followed immediately by nextLine()#

Potentially problematic:

Java
int age = sc.nextInt();
String name = sc.nextLine();

Safer when you want the next full line:

Java
int age = sc.nextInt();
sc.nextLine();
String name = sc.nextLine();

Mistake 4 — Parsing invalid input#

This can fail:

Java
Integer.parseInt("hello");

because "hello" is not an integer.


Mistake 5 — Forgetting that Scanner methods can throw input exceptions#

For example:

Java
sc.nextInt();

expects an integer token.

If the user enters incompatible text, the operation can fail.

Input validation or exception handling is needed for robust programs.


Mistake 6 — Incorrect format specifier#

For example:

Java
double price = 99.5;

System.out.printf("%d", price);

%d is for integer formatting, not a double.

Use:

Java
System.out.printf("%f", price);

or:

Java
System.out.printf("%.2f", price);

67. Important Concept: Input Is Data#

When the user types:

Output
25

the computer receives characters/bytes representing that input.

A Java API then interprets those characters as a particular type.

For example:

"25"
  ↓
Integer.parseInt()
  ↓
25

This distinction is important:

Text representation
       ↓
Parsing
       ↓
Actual numeric value

It is one reason input handling becomes important in larger programs.


68. Why Scanner Is an Object#

You may have noticed:

Java
Scanner sc = new Scanner(System.in);

This is not just a random syntax.

It demonstrates Java's object-oriented design.

Scanner is a class.

Scanner
   ↓
class

Then:

Java
new Scanner(System.in)

creates an object.

Then:

Java
sc.nextInt()

calls a method on that object.

This pattern will become extremely important in the OOP chapters.

You will later learn exactly what:

Java
new

does, what an object reference is, and how objects are represented in memory.


69. A Better Mental Model of Console Input#

Think of console input as a stream:

Keyboard
   ↓
Operating System
   ↓
Standard Input
   ↓
System.in
   ↓
Scanner / BufferedReader
   ↓
Your Java Program

Output works in the opposite direction:

Your Java Program
   ↓
System.out
   ↓
Standard Output
   ↓
Terminal
   ↓
Screen

This is the foundation for understanding file I/O and network I/O later.


70. Standard Streams#

Java programs commonly have:

Output
stdin  → System.in
stdout → System.out
stderr → System.err

You can remember:

Output
in  = input
out = normal output
err = error output

This terminology is common across many programming languages and operating systems.


71. A Small Menu Program#

Let's combine input, output, conditions, and Scanner.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.println("===== MENU =====");
        System.out.println("1. Say Hello");
        System.out.println("2. Show Message");
        System.out.println("3. Exit");

        System.out.print("Enter choice: ");
        int choice = sc.nextInt();

        if (choice == 1) {
            System.out.println("Hello, Java!");
        } else if (choice == 2) {
            System.out.println("Welcome to the Java Master Course!");
        } else if (choice == 3) {
            System.out.println("Goodbye!");
        } else {
            System.out.println("Invalid choice.");
        }

        sc.close();
    }
}

Example:

Output
===== MENU =====
1. Say Hello
2. Show Message
3. Exit
Enter choice: 2
Welcome to the Java Master Course!

This is the beginning of real interactive programs.


72. Mini Project — Student Result Calculator#

Create a program that accepts:

  • student name
  • roll number
  • marks of 3 subjects

Then display:

  • total
  • average
  • percentage

Example solution:

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter student name: ");
        String name = sc.nextLine();

        System.out.print("Enter roll number: ");
        int rollNumber = sc.nextInt();

        System.out.print("Enter marks of subject 1: ");
        double m1 = sc.nextDouble();

        System.out.print("Enter marks of subject 2: ");
        double m2 = sc.nextDouble();

        System.out.print("Enter marks of subject 3: ");
        double m3 = sc.nextDouble();

        double total = m1 + m2 + m3;
        double average = total / 3;

        System.out.println();
        System.out.println("===== RESULT =====");
        System.out.println("Name: " + name);
        System.out.println("Roll Number: " + rollNumber);
        System.out.printf("Total: %.2f%n", total);
        System.out.printf("Average: %.2f%n", average);

        sc.close();
    }
}

73. Mini Project — Bill Generator#

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter product name: ");
        String product = sc.nextLine();

        System.out.print("Enter price: ");
        double price = sc.nextDouble();

        System.out.print("Enter quantity: ");
        int quantity = sc.nextInt();

        double total = price * quantity;

        System.out.println();
        System.out.println("========== BILL ==========");
        System.out.println("Product  : " + product);
        System.out.printf("Price    : %.2f%n", price);
        System.out.println("Quantity : " + quantity);
        System.out.printf("Total    : %.2f%n", total);
        System.out.println("==========================");

        sc.close();
    }
}

Example:

Output
Enter product name: Keyboard
Enter price: 1200
Enter quantity: 2

========== BILL ==========
Product  : Keyboard
Price    : 1200.00
Quantity : 2
Total    : 2400.00
==========================

74. Exercises#

Try these yourself before looking for solutions.

Exercise 1 — Basic Input#

Ask the user for:

Output
name
age
city

Then print:

Output
My name is ___.
I am ___ years old.
I live in ___.

Exercise 2 — Rectangle#

Take:

Output
length
width

Calculate:

Output
area
perimeter

Exercise 3 — Circle#

Take radius and calculate:

Output
area
circumference

Use:

Java
Math.PI

Exercise 4 — Salary#

Take:

Output
basic salary

Calculate:

Output
HRA = 20% of basic
DA  = 10% of basic
Total salary = basic + HRA + DA

Display everything using printf().


Exercise 5 — Temperature#

Take Celsius and convert it to Fahrenheit.

Formula:

Output
F = C × 9 / 5 + 32

Exercise 6 — Character#

Take a character from the user and print:

Output
You entered: X

Hint:

Java
sc.next().charAt(0)

Exercise 7 — Full Name#

Ask for a full name containing spaces.

Make sure the complete name is stored.

Hint:

Java
nextLine()

Exercise 8 — Input Validation#

Ask the user for an integer.

If the input is not an integer, display:

Output
Invalid input

Hint:

Java
hasNextInt()

Exercise 9 — Command-Line Arguments#

Write a program that accepts:

Output
name
age

from command-line arguments and prints:

Output
Name: Aman
Age: 20

Exercise 10 — Mini Calculator#

Take two numbers and print:

Output
Addition
Subtraction
Multiplication
Division
Remainder

75. Challenge Problems#

Challenge 1 — Electricity Bill#

Input:

Output
units consumed

Create a simple electricity bill calculation using your own slab rules.

Display:

Output
Units
Rate
Bill

Use formatted output.


Challenge 2 — Shopping Bill#

Input:

Output
Product 1 price
Product 2 price
Product 3 price

Calculate:

Output
Subtotal
Tax
Final total

Display a clean bill.


Challenge 3 — Student Profile#

Take:

Output
Name
Age
Roll Number
Course
Percentage

Print a properly formatted student profile.


Challenge 4 — Currency Converter#

Take an amount in one currency and convert it using a fixed conversion rate.

The rate can simply be stored as a variable for now.

Do not worry about live exchange rates.


76. Interview Questions#

Q1. What is System.out?#

System.out is the standard output stream used to send normal program output to the console.


Q2. What is System.in?#

System.in is Java's standard input stream, commonly connected to keyboard input when running a console application.


Q3. Difference between print() and println()?#

print() prints without automatically adding a new line.

println() prints and then moves to the next line.


Q4. What is printf()?#

printf() produces formatted output using format specifiers such as %d, %f, %s, and %n.


Q5. What is Scanner?#

Scanner is a class from java.util used to read and parse input from sources such as System.in.


Q6. Difference between next() and nextLine()?#

next() reads the next token.

nextLine() reads the remainder of the current line.


Q7. Why does nextInt() followed by nextLine() cause problems?#

nextInt() reads the integer token but can leave the line separator in the input. The following nextLine() may consume that remaining line separator and return an empty string.

A common solution is:

Java
int n = sc.nextInt();
sc.nextLine();
String line = sc.nextLine();

Q8. Does Java have nextChar() in Scanner?#

No. A common approach is:

Java
char ch = sc.next().charAt(0);

Q9. What does Integer.parseInt() do?#

It converts a String containing a valid integer representation into an int.

Example:

Java
int n = Integer.parseInt("25");

Q10. What happens if parsing fails?#

An invalid numeric string can cause NumberFormatException.

Example:

Java
Integer.parseInt("hello");

Q11. What is BufferedReader?#

BufferedReader is a character-oriented input class from java.io that can efficiently read text, including complete lines.


Q12. Scanner vs BufferedReader?#

Scanner provides convenient token-based parsing and can directly read primitive values.

BufferedReader primarily reads characters/lines, so numeric values commonly need explicit parsing.


Q13. What are command-line arguments?#

They are arguments supplied when starting the Java program and are received through:

Java
String[] args

Q14. What type are command-line arguments?#

They are Strings.

For example:

Java
args[0]

has type:

Java
String

Q15. What is System.err?#

System.err is the standard error stream, commonly used for error and diagnostic messages.


77. Common Interview Trap#

Consider:

Java
System.out.println(10 + 20 + "Java");

Answer:

Output
30Java

Now:

Java
System.out.println("Java" + 10 + 20);

Answer:

Output
Java1020

And:

Java
System.out.println("Java" + (10 + 20));

Answer:

Output
Java30

The reason is left-to-right evaluation combined with String concatenation.


78. Another Interview Trap#

What does this print?

Java
System.out.println(10 / 3);

Answer:

Output
3

because both operands are integers.

But:

Java
System.out.println(10.0 / 3);

prints approximately:

Output
3.3333333333333335

This connects back to the data types and operators chapters.


79. Important Things to Remember#

The most important concepts from this chapter are:

Output
System.in
System.out
System.err

Scanner
next()
nextLine()
nextInt()
nextDouble()
nextBoolean()

printf()
print()
println()

parseInt()
parseDouble()

BufferedReader

Command-line arguments
String[] args

And especially:

Java
nextInt();
sc.nextLine();

when switching from numeric token input to full-line input.


80. Chapter Summary#

Input and output are fundamental because programs need to communicate with the outside world.

For output, Java provides:

Java
System.out.print()
System.out.println()
System.out.printf()

For standard input:

Java
System.in

A beginner-friendly way to read console input is:

Java
Scanner sc = new Scanner(System.in);

Then you can use:

Java
sc.nextInt();
sc.nextDouble();
sc.next();
sc.nextLine();
sc.nextBoolean();

For formatted output:

Java
System.out.printf("%.2f%n", value);

For converting text into numbers:

Java
Integer.parseInt()
Double.parseDouble()

For line-based input:

Java
BufferedReader

Command-line arguments are available through:

Java
String[] args

The most important mental model is:

Diagram
             INPUT
               ↓
       ┌───────────────┐
       │ Java Program  │
       │               │
       │ Processing    │
       └───────────────┘
               ↓
             OUTPUT

Once you understand this flow, you can start building interactive programs rather than programs that only use hard-coded values.


81. Final Chapter Practice Set#

Before moving to Chapter 6, make sure you can write these programs without copying:

  1. Read two integers and print their sum.
  2. Read two numbers and print all arithmetic operations.
  3. Read a full name and print it.
  4. Read age and percentage and display them using printf().
  5. Read three marks and calculate total and average.
  6. Read radius and calculate circle area.
  7. Read temperature and convert it.
  8. Read a character.
  9. Create a simple menu-driven program.
  10. Validate integer input using hasNextInt().
  11. Read a number from args.
  12. Convert a String to an integer using Integer.parseInt().
  13. Understand the nextInt() + nextLine() issue.
  14. Explain Scanner vs BufferedReader.
  15. Explain the difference between System.in, System.out, and System.err.

If you can solve these comfortably, your basic Java input/output foundation is strong.


82. Connection to the Rest of the Course#

This chapter uses concepts from earlier chapters:

Diagram
Chapter 1
Java Introduction
      ↓
Chapter 2
Setup & First Program
      ↓
Chapter 3
Variables & Data Types
      ↓
Chapter 4
Operators
      ↓
Chapter 5
Input & Output
      ↓
Chapter 6
Conditions

From this point onward, your programs become increasingly interactive.

For example:

Input
  ↓
Condition
  ↓
Decision
  ↓
Output

Then later:

Input
  ↓
Loops
  ↓
Methods
  ↓
Arrays
  ↓
Objects
  ↓
OOP

This is how the course gradually moves from basic Java syntax toward real software development.


83. What Comes Next?#

The next chapter is:

Chapter 6 — Conditions#

You will learn how Java makes decisions using:

Java
if
if-else
else-if
nested if
switch
switch expressions
ternary operator

and how to build decision-making programs using the input techniques learned in this chapter.