SoFunction
Updated on 2025-03-08

Java uses Scanner class to implement console input method

In Java, it is not just like Python that just uses input(). Java console input is troublesome, and the following is a relatively easy-to-understand tutorial.

First, create a new class that calls Java's Scanner and create a new main method.

import ;
public class scanner
{
 public static void main(String[] args)
 {

 }
}

Create a new Scanner object instance in the main method.

Scanner enter = new Scanner();

Finally, create a new data type in the main method. As for what type it is, it depends on what type it is input.

//The input is int typeint inputi = ();

//The input is double typedouble inputd = ();

//The input is double typeString inputs = ();

Note: The class name, variable name, and object instance name (ellipsis) in this tutorial can be replaced. Let's run the program.

Here I will bring you a password program made with Scanner.

import ;
public class pswd
{
 public static void main(String[] args)
 {
 ("Please enter your password:");
 Scanner enter = new Scanner();
 int password = ();
 if(password == 123456)
 {
  ("The input is correct!");
 }
 else
 {
  ("Input error!");
 }
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.