Explain Codes LogoExplain Codes Logo

How to get the user input in Java?

java
input-methods
scanner-class
user-input
Anton ShumikhinbyAnton Shumikhin·Jan 7, 2025
TLDR

Here's a simple way to get text input using Java's Scanner:

// Importing Scanner - your gateway to user inputs import java.util.Scanner; Scanner scanner = new Scanner(System.in); // Setting up a bridge between user and your program System.out.print("Give me something: "); // Asking politely for input :) String input = scanner.nextLine(); // Catching user input like a pro System.out.println("Gotcha: " + input); // Announcement of triumph! scanner.close(); // Always remember to close the doors when you leave.

Deep diving into user inputs

Eating up different types of inputs

The Scanner class is like a chameleon, it can morph according to the input type. It has nextInt() for integer, nextDouble() for double, and so on:

int yourNumber = scanner.nextInt(); // Scanner acting as an integer catcher double yourFraction = scanner.nextDouble(); // Scanner showing off its double catching talent

Don't forget to prompt the user before getting each input, manners matter even in code!

System.out.print("May I know your favourite number?: "); // All hail the magic conch! // Then read the number

Don't leave your scanner open

Alas the busy Scanner, if forgotten to be closed, can cause resource leaks:

scanner.close(); // They said there were leaks; I said the Scanner's closed.

Rolling with the unexpected

The seas of user inputs can be treacherous. Handle exceptions well when user sends something unexpected:

try { // Hoping to catch user input but ready for surprises } catch (InputMismatchException e) { // See, I told you, they are full of surprises! }

Exploring the alternate realities

Not a fan of Scanner or want more spice? Use Console for interactive prompts and password inputs, or choose BufferedReader for large inputs:

Console console = System.console(); String codeName = console.readLine("Tell me your code name: "); char[] secretPassword = console.readPassword("Whisper your password: ");

Beware, shh, hear it from the wise, Console might not work in some IDEs.

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String novel = reader.readLine(); // Because why not read a novel from the console?

Gift wrapping whole user interaction

Dealing with past - Deprecated methods

Beware of the past! Deprecated methods like readLine from DataInputStream might look tempting but can bite. Remember - age sometimes comes without wisdom.

Building your own Jarvis

Why not use this newfound knowledge to do something cool - like a simple console calculator!

System.out.print("Tell Jarvis your operation (+, -, *, /): "); // Jarvis is your command! String operation = scanner.nextLine(); // Moving on with providing operands...

Select the right tool (method), for the right job (expected input type):

int operand1 = scanner.nextInt(); // Operand is integer, use nextInt() double operand2 = scanner.nextDouble(); // Operand is double, do the nextDouble()

Advanced etiquettes - Inline prompts

"Can you put the prompt right next to my input?" - Sure, here it is!

System.out.print("You know the drill, Enter your name: "); // Prompt and input on the same line, elegance redefined! String name = scanner.nextLine();

The cultural nuances

Different locales have different ways of reading numbers. Ensure consistency by setting your scanner's locale:

scanner.useLocale(Locale.US); // Because sometimes, 1,000 does not mean one thousand.