Input to the Java console

Input to the console

Entering a value in the console and using that value in your program. A library called Scanner is used to receive input to the console. Scanner uses "import java.util.Scanner". [Example]

import java.util.Scanner;
class Main {
  public static void main(String[]args) {
  Scanner scanner = new Scanner(;  //Initialize with Scanner
  String name =;  //Receiving string input

Initialize with new Scanner ( ;. Receives string input with () ;.

How to use Scanner

First, the Scanner is initialized and put in a variable called the scanner. The Scanner calls the method using the variable assigned to this initialized one. Receive the string entered in the console with (). [Example]

import java.util.Scanner;
class Main {
  public static void main (String[] args) {
    Scanner scanner = new Scanner(;
    String name =;
    System.out.println(name + "Mr.");

When you execute the above, "Name:" will be displayed, so if you enter a character string there, it will be output as Mr. 〇〇.

How to receive numerical input

Use the Scanner as before. The method that receives an integer uses the nextInt method, and the method that receives a decimal uses the nextDouble method. [Example]

import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(;
    String firstName =;

    System.out.print("Last name:");
    String lastName =;

    int age = scanner.nextInt();  //Receive an integer with the nextInt method
    double height = scanner.nextDouble();  //Receive a decimal with the nextDouble method
    System.out.print("body weight(kg):");
    double weight = scanner.nextDouble();
    Person.printData(Person.fullName(firstName, lastName), age, height, weight);

Recommended Posts

Input to the Java console
Console input in Java (understanding the mechanism)
[java8] To understand the Stream API
[Java] Input to stdin of Process
Welcome to the Java Library Swamp! !!
[Java] How to use the File class
Java reference to understand in the figure
Introduction to java for the first time # 2
[Java] How to use the hasNext function
Java SE8 Silver ~ The Road to Pass ~
[Java] How to use the HashMap class
About the procedure for java to work
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Java] How to set the Date time to 00:00:00
[Java] How to get the current directory
[Java] Introduction to Java
How to install the legacy version [Java]
How to get the date in java
Output of the book "Introduction to Java"
[Processing × Java] How to use the function
I went to the Java Women's Club # 1
[Java] Color the standard output to the terminal
[Java] How to use the Calendar class
I tried to display the calendar on the Eclipse console using Java.
[Java] How to use Thread.sleep to pause the program
[Java] How to get and output standard input
How to use the replace () method (Java Silver)
[Java / PostgreSQL] Connect the WEB application to the database
[Java] How to get the redirected final URL
[Java] Memo on how to write the source
[Java] How to get the authority of the folder
[Java] Try to solve the Fizz Buzz problem
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Kotlin's improvements to Java
Introduction to java command
[Java] How to get the URL of the transition source
[Java] How to omit the private constructor in Lombok
[Java] I want to calculate the difference from the date
How to write Scala from the perspective of Java
How to input / output IBM mainframe files in Java?
[Java] How to extract the file name from the path
[Java] How to get the maximum value of HashMap
Source used to get the redirect source URL in Java
[Android Studio] [Java] How to fix the screen vertically
Java classes and instances to understand in the figure
HTTPS connection with Java to the self-signed certificate server
Java: Use Stream to sort the contents of the collection
I tried to implement the Euclidean algorithm in Java
How to get the class name / method name running in Java
[Java] Change the process according to the situation with the Strategy pattern
[Java] How to use Map
How to lower java version
Migration from Cobol to JAVA
[Java] How to use Map
9 Corresponds to the return value
Convert Java Powerpoint to XPS
Java adds table to PDF
How to uninstall Java 8 (Mac)
Java to play with Function