Java User Input (Scanner) Tutorial šŸŽÆ

beginner
7 min

Java User Input (Scanner) Tutorial šŸŽÆ

Welcome to our comprehensive guide on Java User Input using the Scanner class! This tutorial is designed to help both beginners and intermediate learners understand the concept from scratch. Let's dive right in!

Understanding User Input šŸ“

In programming, interacting with users is essential. Java provides the Scanner class to read user inputs from the keyboard.

java
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // User input code here } }

šŸ’” Pro Tip: Always start your Java projects by importing necessary packages and defining the main method.

Reading Basic Inputs šŸ’”

Reading a Single Line

The nextLine() method reads a single line from the user, which includes spaces, special characters, and line breaks.

java
Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Hello, " + name);

Reading Individual Words

If you want to read individual words from a single line, use the split() method.

java
Scanner scanner = new Scanner(System.in); System.out.print("Enter your name and age: "); String[] inputs = scanner.nextLine().split(" "); String name = inputs[0]; int age = Integer.parseInt(inputs[1]); System.out.println("Hello, " + name + ". You are " + age + " years old.");

Reading a Single Word

For reading a single word, we can use the next() method, but it does not recognize line breaks, so it may cause issues in some cases.

java
Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.next(); System.out.println("Hello, " + name);

Reading Numeric Inputs šŸ’”

Reading an Integer

To read an integer, use the nextInt() method. It's essential to validate the input, as nextInt() throws an exception when the user enters a non-integer value.

java
Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int number = scanner.nextInt(); System.out.println("You entered the number " + number);

Reading a Double

To read a double, use the nextDouble() method. Like nextInt(), it also throws an exception when the user enters a non-double value.

java
Scanner scanner = new Scanner(System.in); System.out.print("Enter a double: "); double number = scanner.nextDouble(); System.out.println("You entered the number " + number);

Handling User Input Errors šŸ’”

When reading numeric inputs, it's essential to handle user input errors by using try-catch blocks.

java
Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); try { int number = scanner.nextInt(); System.out.println("You entered the number " + number); } catch (InputMismatchException e) { System.out.println("Invalid input. Please enter an integer."); }

Quiz šŸ’”

Quick Quiz
Question 1 of 1

What method should be used to read a single line from the user in Java?

That's it for our Java User Input tutorial! Remember to practice reading different types of inputs and handling user errors to become proficient in using the Scanner class. Happy coding! šŸ’”