Acquisition of input contents using Scanner (Java)

What is Scanner

An external library for "entering" a value into the console and using that value in your program. This article describes how to receive a string and how to receive a number.

How to receive as a string

The procedure for receiving a character string is as follows.

  1. Load the library
  2. Scanner initialization
  3. Processing to receive input contents

Loading the library

In order to use Scanner, a description for loading an external library is required. First, add the description of import java.util.Scanner; above the class definition.

Main.java


import java.util.Scanner;

class Main {
  public static void main (String[] args) {
    System.out.print("name:");
  }
}

Scanner initialization

Next, add the description Scanner scanner = new Scanner (System.in); to initialize the Scanner. The Scanner is initialized with new Scanner (System.in) and assigned to the variable scanner.

Main.java


import java.util.Scanner;

class Main {
  public static void main (String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("name:");
  }
}

Processing to receive input contents

Finally, add the description String name = scanner.next () ;. You can substitute the character string entered in the variable name by writing scanner.next ().

Main.java


import java.util.Scanner;

class Main {
  public static void main (String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("name:");
    String name = scanner.next();
    System.out.println("Hello" + name + "Mr.");
  }
}

How to receive numerically

The procedure for receiving numerical values is also as follows, but 1 and 2 are omitted because there is a common part with the procedure for receiving numerical strings.

  1. Load the library
  2. Scanner initialization
  3. Processing to receive input contents

Processing to receive input contents

We will add a description to receive numerical values in the following file. (Steps 1 and 2 have been completed)

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("age:");
    
    System.out.print("height(m):");
    
  }
}

To receive the age value int age = scanner.nextInt(); Add the description of. The data type is specified by writing nextInt () as a difference from the procedure for receiving as a character string. Note that if you write nextString () etc. when receiving as a character string, an error will occur.

Main.java


import java.util.Scanner;

class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    
    System.out.print("age:");
    int age = scanner.nextInt();
    
    System.out.print("height(m):");
    double height = scanner.nextDouble();

  }
}

Recommended Posts

Acquisition of input contents using Scanner (Java)
java Scanner loop input
[java.io] Summary of Java string input (InputStream, Reader, Scanner)
Talk about using Java input wait (Scanner) in VS Code
[Java] Input to stdin of Process
Acquisition of location information using FusedLocationProviderClient
Summary of object-oriented programming using Java
I tried using GoogleHttpClient of Java
Story of paiza.jp [Java standard input solution]
Check the contents of the Java certificate store
Memo: [Java] Check the contents of the directory
Study Java Try using Scanner or Map
[Java] Contents of Collection interface and List interface
JAVA: jar, aar, view the contents of the file
java Scanner class
Verification of performance impact when using Java volatile
Try similar search of Image Search using Java SDK [Search]
Example of using addition faster than using StringBuilder (Java)
Story of test automation using Appium [Android / java]
[Java] Overview of Java
[Android] Display of input candidates using List Popup Window
Rails6: Input the initial data of ActionText using seed
Java: Use Stream to sort the contents of the collection
[Java10] Be careful of using var and generics together
Expired collection of java
Sorting using java comparator
Predicted Features of Java
[Java] Significance of serialVersionUID
NIO.2 review of java
Review of java Shilber
java --Unification of comments
Scraping practice using Java ②
History of Java annotation
Scraping practice using Java ①
NIO review of java
[Java] Three features of Java
Summary of using DBFlow
Summary of Java support 2018
I didn't understand the behavior of Java Scanner and .nextLine ().
[Rails] Implementation of automatic address input using jpostal and jp_prefecture
[Java] Comparison method of character strings and comparison method using regular expressions
Using multiple versions of Java with Brew on Mac + jEnv
Check the status of Java application without using monitoring tool
[Java] Try editing the elements of the Json string using the library
The story of pushing Java to Heroku using the BitBucket pipeline