How to Take Input from User in Java: Simple Guide
In Java, you can take input from the user using the
Scanner class from java.util package. Create a Scanner object linked to System.in, then use methods like nextLine() or nextInt() to read input.Syntax
To take input from the user, first import the Scanner class. Then create a Scanner object connected to the keyboard input stream System.in. Use methods like nextLine() for text or nextInt() for numbers.
- import java.util.Scanner; - imports the Scanner class.
- Scanner sc = new Scanner(System.in); - creates a Scanner object named
sc. - sc.nextLine() - reads a full line of text.
- sc.nextInt() - reads an integer number.
java
import java.util.Scanner; public class InputExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Create Scanner object String input = sc.nextLine(); // Read a line of text int number = sc.nextInt(); // Read an integer sc.close(); // Close the scanner } }
Example
This example asks the user to enter their name and age, then prints a greeting message using the input values.
java
import java.util.Scanner; public class UserInputExample { 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."); sc.close(); } }
Output
Enter your name: Alice
Enter your age: 30
Hello, Alice. You are 30 years old.
Common Pitfalls
One common mistake is mixing nextLine() with other Scanner methods like nextInt(). After reading a number, the newline character remains in the input buffer, causing nextLine() to read an empty string.
To fix this, add an extra sc.nextLine() after reading numbers to consume the leftover newline.
java
import java.util.Scanner; public class PitfallExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); sc.nextLine(); // consume leftover newline // Corrected: nextLine() reads user input after consuming newline System.out.print("Enter your name: "); String name = sc.nextLine(); System.out.println("Name: " + name + ", Age: " + age); sc.close(); } } // Original incorrect version: /* System.out.print("Enter your age: "); int age = sc.nextInt(); // Missing sc.nextLine() here causes issues System.out.print("Enter your name: "); String name = sc.nextLine(); */
Quick Reference
- Import Scanner:
import java.util.Scanner; - Create Scanner:
Scanner sc = new Scanner(System.in); - Read String:
sc.nextLine() - Read int:
sc.nextInt() - Close Scanner:
sc.close();
Key Takeaways
Use Scanner class with System.in to read user input in Java.
Use nextLine() for strings and nextInt() for integers.
After nextInt(), call nextLine() to consume leftover newline before reading strings.
Always close the Scanner object to free resources.
Import java.util.Scanner before using Scanner.