Read Input From Console Java Until User Enters
- Details
- Written by
- Last Updated on 27 July 2019 | Print E-mail
In this commodity, nosotros discuss iii different ways for reading input from the user in the command line environment (also known equally the "console"). Each mode is fairly easy to use and also has its own advantages and drawbacks.
1. Reading User's Input using BufferedReader course
By wrapping the System.in (standard input stream) in an InputStreamReader which is wrapped in a BufferedReader , we tin read input from the user in the command line. Here's an instance:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Organisation.out.print("Enter your proper noun: "); String name = reader.readLine(); System.out.println("Your proper name is: " + proper name); In the above example, the readLine() method reads a line of text from the command line.
Advantages: The input is buffered for efficient reading.
Drawbacks: The wrapping lawmaking is hard to remember.
2. Reading User's Input using Scanner class
The main purpose of the Scanner course (available since Java 1.v) is to parse archaic types and strings using regular expressions, yet it is as well tin be used to read input from the user in the command line. Hither's an example:
Scanner scanner = new Scanner(Arrangement.in); Organisation.out.impress("Enter your nationality: "); String nationality = scanner.nextLine(); System.out.impress("Enter your age: "); int age = scanner.nextInt(); Advantages:
- Convenient methods for parsing primitives (nextInt(), nextFloat(), …) from the tokenized input.
- Regular expressions can be used to find tokens.
Drawbacks:
- The reading methods are not synchronized.
Learn more: Java Scanner Tutorial and Lawmaking Examples
3. Reading User's Input using Console class
The Console class was introduced in Java 1.half-dozen, and it has been becoming a preferred mode for reading user's input from the command line. In addition, it can be used for reading password-like input without echoing the characters entered by the user; the format string syntax can also exist used (similar Organization.out.printf()). Here'southward an example lawmaking snippet:
Console panel = System.console(); if (panel == null) { Organization.out.println("No console: non-interactive mode!"); System.exit(0); } System.out.print("Enter your username: "); String username = panel.readLine(); System.out.print("Enter your countersign: "); char[] password = console.readPassword(); String passport = console.readLine("Enter your %d (th) passport number: ", 2); Advantages:
- Reading password without echoing the entered characters.
- Reading methods are synchronized.
- Format cord syntax can exist used.
Drawbacks:
- Does not work in non-interactive surroundings (such as in an IDE).
Larn more: Coffee Console Input Output Examples
four. Java Reading User's Input Example Plan
For your convenient and reference purpose, we combine the above code snippet into a demo plan whose source code looks like this:
packet cyberspace.codejava.io; import java.io.*; import coffee.util.*; public grade UserInputConsoleDemo { public static void main(String[] args) { // using InputStreamReader try { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Arrangement.out.print("Enter your name: "); String name = reader.readLine(); System.out.println("Your name is: " + name); } catch (IOException ioe) { ioe.printStackTrace(); } // using Scanner Scanner scanner = new Scanner(System.in); Organization.out.print("Enter your nationality: "); Cord nationality = scanner.nextLine(); System.out.println("Your nationality is: " + nationality); // using Console Console panel = System.console(); if (console == null) { System.out.println("No console: not in interactive fashion!"); Arrangement.get out(0); } System.out.print("Enter your username: "); String username = console.readLine(); System.out.print("Enter your password: "); char[] countersign = console.readPassword(); System.out.println("Thank you!"); Arrangement.out.println("Your username is: " + username); System.out.println("Your password is: " + String.valueOf(password)); // using Panel with formatted prompt String job = console.readLine("Enter your chore: "); Cord passport = console.readLine("Enter your %d (th) passport number: ", 2); System.out.println("Your job is: " + job); Arrangement.out.println("Your passport number is: " + passport); } }Watch the video:
References:
- Console class Javadoc
- I/O from the Command Line - The Java Tutorials
- Scanner grade Javadoc
- BufferedReader form Javadoc
Related Tutorials:
- Coffee Console Input Output Examples
- Java Scanner Tutorial and Lawmaking Examples
Other Coffee File IO Tutorials:
- How to Read and Write Text File in Coffee
- How to Read and Write Binary Files in Java
- Coffee IO - Common File and Directory Operations Examples
- Java Serialization Basic Example
- Understanding Coffee Externalization with Examples
- How to compress files in Nothing format in Java
- How to extract Nil file in Java
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Coffee 1.iv and has been falling in love with Java since then. Brand friend with him on Facebook and watch his Java videos you lot YouTube.
Add together comment
Source: https://www.codejava.net/java-se/file-io/3-ways-for-reading-input-from-the-user-in-the-console
0 Response to "Read Input From Console Java Until User Enters"
Post a Comment