java Scanner class

About the Scanner class

Introduction

The Scanner class is a class included in the java.util package.

It is mainly used for inputting console screens and reading csv files.

I'm tired of reading the explanation, so I'll go to the example. スクリーンショット 2020-05-26 15.37.27.png

example

First, let's take a look at the frequently used type specification input. Code screen スクリーンショット 2020-05-26 14.32.14.png

Console screen スクリーンショット 2020-05-26 14.32.20.png

Yes, it looks like this. By the way, this is incomplete, but it is easy to see, so I will explain it once.

As for the order ① First, import the Scanner class. The import rule is ```import package name.class name` ``. ② Next, [instantiate] the imported Scanner class (https://techacademy.jp/magazine/17543). System.in is the console input (standard input).

③ Create a variable of the specified type. Get the value there with the nextInt () method of the Scanner class and initialize it.

`Other types include nextByte, nextShort, nextLong, nextFloat, nextDouble, and nextBoolean. There is only a char type method. There are next and nextLine in the character string. ``

④ Finally, you need to close the Scanner with the close method. Be careful not to close it as it will cause an unexpected error.

Exception handling

The fact that it is incomplete means that an error will occur if you input anything other than int type on the above console input screen. Console screen スクリーンショット 2020-05-26 15.17.01.png

If you try to perform an unexpected process in this way, an error will occur, so you should expect the process in this case. (I don't understand Japanese)

The try-catch syntax covers the handling of possible exceptions. The procedure is the same as the if statement.

Handling that may cause exceptions;
}catch(<Exception class> <Variable name>){
What to do when an exception occurs;
}```
 is. This time

 code
 <img width="704" alt="スクリーンショット 2020-05-26 15.24.23.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/253875/3d2e96d6-1b6e-e20e-c542-17ac611c6e2d.png ">

 Console screen
 <img width="469" alt="スクリーンショット 2020-05-26 15.24.35.png " src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/253875/babbcf5c-907a-a78f-3fa4-f1358352f71f.png ">


 As a caveat
①
 You need to import the exception handling class.
 If you look closely, you can see that ```import java.util.InputMismatchException; // `` `for exception handling has been added at the top.

②

#### **`System.out.println("The entered number is" + num + "is.");Try-If you put it in the catch statement, it will process even if it is an exception.(This time the scope of num is inside the try block, so an error occurs)`**
スクリーンショット 2020-05-26 15.35.29.png This is unpleasant ~.

Summary

I tried to summarize how to use the super basic Scanner. The following is a compilation of code articles that loop input until normal processing is done.

Next

Recommended Posts

java Scanner class
Java class methods
[Java] Class inheritance
Java HashMap class
java (abstract class)
[Java] Nested class
Java anonymous class
About Java class
[java] abstract class
[Java] Object class
Java local class
How to use Java Scanner class (Note)
About class division (Java)
About Java StringBuilder class
[Java] About Singleton Class
Java inner class review
Java class type field
Java programming (class method)
About Java String class
Java programming (class structure)
About java abstract class
java Scanner loop input
[Java] Integer wrapper class reference
Java memo (standard class) substring
Java tips --StaticUtility class modifier
Java inflexible String class substring
Java memo (standard class) length
[Implementation] Java Process class notes
About Java class loader types
scanner
How to use java class
[Java] Comparator of Collection class
Java class definition and instantiation
Java
Java learning memo (abstract class)
Summary of Java Math class
[Java] What is class inheritance?
About Java class variables class methods
Java
[Java basics] What is Class?
Create an immutable class with JAVA
Why are class variables needed? [Java]
Call Kotlin's sealed class from Java
[Java] Object-oriented syntax --class method / argument
Various methods of Java String class
[Java] Memo for naming class names
How to disassemble Java class files
Kotlin Class to send to Java developers
How slow is a Java Scanner?
StringBuffer and StringBuilder Class in Java
How to decompile java class files
[Java] How to use LinkedHashMap class
java (use class type for field)
Java source code reading java.lang.Math class
[Java] Button template class by Graphics
How to use class methods [Java]
[Java] How to use Math class
Studying Java ―― 3
[Java] array
What is a class in Java language (3 /?)
Java protected