0% found this document useful (0 votes)
3 views2 pages

Lesson 4 Java Learning Guide - Input and Output in Java

This guide introduces beginners to input and output in Java using the Scanner class and System.out.println. It explains how to print text, read user input, and provides examples of common Scanner methods. Additionally, it offers a mini project idea for creating an interactive Java program.

Uploaded by

Ramya Sajeeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Lesson 4 Java Learning Guide - Input and Output in Java

This guide introduces beginners to input and output in Java using the Scanner class and System.out.println. It explains how to print text, read user input, and provides examples of common Scanner methods. Additionally, it offers a mini project idea for creating an interactive Java program.

Uploaded by

Ramya Sajeeth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Learning Guide – Input and Output in Java

Description:
This beginner-level guide explains how to get input from the user and display output in Java.
It uses the Scanner class for input and System.out.println for output. Great for students
starting interactive Java programs.

🖥️Output in Java

To print text or variables to the screen, we use:

java
CopyEdit
System.out.println("Hello, World!");
System.out.print("Enter your name: ");

 println() moves to the next line.


 print() keeps the cursor on the same line.

🔽 Input in Java using Scanner

To read user input in Java, we must import and use the Scanner class from the java.util
package.

✏️Example:
java
CopyEdit
import java.util.Scanner;

public class InputExample {


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

System.out.print("Enter your name: ");


String name = sc.nextLine();

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


int age = sc.nextInt();

System.out.println("Hello, " + name + "! You are " + age + " years
old.");
}
}

🧠 Common Scanner Methods


Method Reads Example Input

nextInt() Integer 25

nextDouble() Decimal number 5.75

next() Single word Hello

nextLine() Full line Hello there

🛑 Tip: Clear Input Buffer

When switching from nextInt() to nextLine(), add an extra sc.nextLine() to clear the
newline character:

java
CopyEdit
int age = sc.nextInt();
sc.nextLine(); // consume leftover newline
String name = sc.nextLine();

✅ Mini Project Idea

Create a Java program that:

 Asks the user for their name


 Asks their favorite color
 Displays a greeting with that color and name

You might also like