Reading inputs in java

Reading inputs in java

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java

Источник

Reading inputs in java

  • Introduction to Java
  • The complete History of Java Programming Language
  • C++ vs Java vs Python
  • How to Download and Install Java for 64 bit machine?
  • Setting up the environment in Java
  • How to Download and Install Eclipse on Windows?
  • JDK in Java
  • How JVM Works – JVM Architecture?
  • Differences between JDK, JRE and JVM
  • Just In Time Compiler
  • Difference between JIT and JVM in Java
  • Difference between Byte Code and Machine Code
  • How is Java platform independent?
  • Decision Making in Java (if, if-else, switch, break, continue, jump)
  • Java if statement with Examples
  • Java if-else
  • Java if-else-if ladder with Examples
  • Loops in Java
  • For Loop in Java
  • Java while loop with Examples
  • Java do-while loop with Examples
  • For-each loop in Java
  • Continue Statement in Java
  • Break statement in Java
  • Usage of Break keyword in Java
  • return keyword in Java
  • Object Oriented Programming (OOPs) Concept in Java
  • Why Java is not a purely Object-Oriented Language?
  • Classes and Objects in Java
  • Naming Conventions in Java
  • Java Methods
  • Access Modifiers in Java
  • Java Constructors
  • Four Main Object Oriented Programming Concepts of Java
  • Inheritance in Java
  • Abstraction in Java
  • Encapsulation in Java
  • Polymorphism in Java
  • Interfaces in Java
  • ‘this’ reference in Java
Читайте также:  Scale function in javascript

Источник

Java User Input (Scanner)

The Scanner class is used to get user input, and it is found in the java.util package.

To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:

Example

import java.util.Scanner; // Import the Scanner class class Main < public static void main(String[] args) < Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input >> 

If you don’t know what a package is, read our Java Packages Tutorial.

Input Types

In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:

Method Description
nextBoolean() Reads a boolean value from the user
nextByte() Reads a byte value from the user
nextDouble() Reads a double value from the user
nextFloat() Reads a float value from the user
nextInt() Reads a int value from the user
nextLine() Reads a String value from the user
nextLong() Reads a long value from the user
nextShort() Reads a short value from the user

In the example below, we use different methods to read data of various types:

Example

import java.util.Scanner; class Main < public static void main(String[] args) < Scanner myObj = new Scanner(System.in); System.out.println("Enter name, age and salary:"); // String input String name = myObj.nextLine(); // Numerical input int age = myObj.nextInt(); double salary = myObj.nextDouble(); // Output input by user System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); >> 

Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error message (like «InputMismatchException»).

Читайте также:  Map no duplicates java

You can read more about exceptions and how to handle errors in the Exceptions chapter.

Источник

Reading User Input from Console in Java

In this Java tutorial, we will learn how to read the user input text from the console in Java. Reading console input in programs may be necessary to make applications interactive.

Console class is from java.io package and is used to read from and write to the character-based console.

The System.console() is used to get the reference of the system console. Note that if JVM has been launched with a background job then the program will not have a console. In this case, invocation of System.console() method will return null .

  • The readLine() reads a single line of text from the console.
  • The readLine(line) writes the line into the console and then reads the user input from the console.
  • The readPassword() is used to read the secure input. For example, passwords and encryption keys.
  • The readPassword(line) prompts the line into the console and reads the secure user input. For example, passwords and encryption keys.
  • Passing a null argument to any method in this class will cause a NullPointerException to be thrown.
Console console = System.console(); String inputString = console.readLine("Enter Your Name: "); System.out.println("The name entered: " + inputString);
Enter Your Name: Lokesh The name entered: Lokesh

BufferedReader is supported since Java 1.1. We may see its usage in legacy Java applications. To read console input, we shall wrap the System.in (standard input stream) in an InputStreamReader which again wrapped in a BufferedReader class.

BufferedReader reads text from the console, buffering characters so as to provide for the efficient reading of user input. It makes the read operations from InputStreamReader – less costly.

System.out.print("Enter Your Name: "); //Prompt BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in)); String inputString = bufferRead.readLine(); System.out.println("The name entered: " + inputString);
Enter Your Name: Lokesh The name entered: Lokesh

In Java, System.in represents the standard input. By default, it is the system console.

Читайте также:  Exploit python agent w

The Scanner class, when reading from the console, provides methods to read different types of data e.g. integers, numbers, strings, etc.

Scanner scanner = new Scanner(System.in); System.out.println("Enter name, age and salary:"); String name = scanner.nextLine(); int age = scanner.nextInt(); double salary = scanner.nextDouble(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary);

Above all techniques are equally effective, but I personally like the java.io.Console way. It simply makes code more readable. What is your choice to read the test from Console in Java.

Источник

Оцените статью