⬅ Previous Topic
Java Operator PrecedenceNext Topic ⮕
Java Conditional Statements⬅ Previous Topic
Java Operator PrecedenceNext Topic ⮕
Java Conditional StatementsWhen you first write a Java program, it’s natural to want interaction — a two-way conversation between the user and the machine. Java enables this through standard input and output streams. This tutorial will walk you through how to accept user input and print output using beginner-friendly and practical approaches.
Let’s begin with the most common way to display information in Java: System.out.print()
and System.out.println()
.
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello ");
System.out.print("World");
System.out.println("!");
}
}
Hello World!
System.out.print()
prints without moving to the next line, whereas System.out.println()
adds a new line after printing. It's essential to use the right one depending on whether you want continued or separate output lines.
To accept input from the keyboard, Java commonly uses the Scanner
class from the java.util
package.
import java.util.Scanner;
You create a Scanner object using:
Scanner input = new Scanner(System.in);
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = input.nextLine();
System.out.println("Hello, " + name + "!");
}
}
Enter your name: Alice
Hello, Alice!
input.nextLine()
reads a full line of text. Scanner provides different methods for different types of input:
nextInt()
– for integersnextDouble()
– for floating-point numbersnext()
– for single word (stops at whitespace)import java.util.Scanner;
public class SumCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = input.nextInt();
System.out.print("Enter second number: ");
int num2 = input.nextInt();
int sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
Enter first number: 10
Enter second number: 20
Sum: 30
While using nextInt()
, if the user inputs something non-numeric, your program will throw an InputMismatchException
. To handle such cases, use input validation:
if (input.hasNextInt()) {
int value = input.nextInt();
} else {
System.out.println("Invalid input. Please enter a number.");
}
If you use nextLine()
after nextInt()
, you might skip input. This happens because nextInt()
doesn’t consume the newline character. Use an extra nextLine()
to clear the buffer:
input.nextLine(); // consume leftover newline
Input and output are the lifeline of any interactive program. Mastering these early will help you write programs that engage users. From reading simple text to complex validations — practice makes perfect.
System.out.print()
and System.out.println()
for outputScanner
for reading user inputnextLine()
, nextInt()
, nextDouble()
, etc.
System.out.print("Java ");
System.out.print("is ");
System.out.println("fun!");
nextInt()
followed by nextLine()
can sometimes result in skipping user input.Scanner
class are accurate?⬅ Previous Topic
Java Operator PrecedenceNext Topic ⮕
Java Conditional StatementsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.