In this tutorial, we will learn how to use the Java Scanner class to read user input. We will see how to read login and registration data from console or user input.
Scanner class in Java is found in the java.util package. Java provides various ways to read input from the keyboard, the java.util.Scanner class is one of them.
The Java Scanner class breaks the input into tokens using a delimiter which is whitespace by default. It provides many methods to read and parse various primitive values.
Read more about Scanner class at https://www.javaguides.net/2020/02/java-scanner-class-example-tutorial.html
Let's first understand how the Scanner class works.
How does a Scanner work?
Basically, a Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace (blanks, tabs, and line terminators). The parsed tokens can be converted into primitive types and Strings using various next methods.
Here’s the simplest example of using a Scanner to read String from the user:
System.out.println("Enter string input: ");
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
Java Scanner - Login Example
In the below example
- we read userName and password input from the console
- valid with hardcoded username and password
- we display a login success message if a username and password matches otherwise we display an error message.
package com.java.tutorials.projects.login;
import java.util.Scanner;
public class Login {
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print(" Enter user name => ");
String userName = scanner.nextLine();
System.out.print(" Enter password => ");
String password = scanner.nextLine();
if ("ramesh".equals(userName) && "password".equals(password)) {
System.out.println(" User successfully logged-in.. ");
} else {
System.out.println(" In valid userName of password ");
}
}
}
}
Output:
Enter user name => ramesh
Enter password => password
User successfully logged-in..
Java Scanner - Registration Example
In the below example, we will see how use Scanner class to read user registration data from console and we populate all input data into Register class object.
package com.java.tutorials.projects.login;
import java.util.Scanner;
public class Registration {
static Register register = new Register();
public static void main(String[] args) {
try (Scanner scanner = new Scanner(System.in)) {
System.out.print(" Enter firstName => ");
String firstName = scanner.nextLine();
register.setFirstName(firstName);
System.out.print(" Enter lastName => ");
String lastName = scanner.nextLine();
register.setLastName(lastName);
System.out.print(" Enter userName => ");
String userName = scanner.nextLine();
register.setUserName(userName);
System.out.print(" Enter password => ");
String password = scanner.nextLine();
register.setPassword(password);
System.out.print(" Enter emailId => ");
String emailId = scanner.nextLine();
register.setEmailId(emailId);
System.out.print(" Enter phoneNo => ");
long phoneNo = scanner.nextLong();
register.setPhoneNo(phoneNo);
System.out.println(register.toString());
}
}
}
class Register {
private String firstName;
private String lastName;
private String userName;
private String password;
private String emailId;
private long phoneNo;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
public long getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(long phoneNo) {
this.phoneNo = phoneNo;
}
@Override
public String toString() {
return "Register [firstName=" + firstName + ", lastName=" + lastName + ", userName=" + userName + ", password=" +
password + ", emailId=" + emailId + ", phoneNo=" + phoneNo + "]";
}
}
Output:
Enter firstName => Ramesh
Enter lastName => Fadatare
Enter userName => Ramesh Fadatare
Enter password => Password
Enter emailId => ramesh@gmail.com
Enter phoneNo => 1234567899
Register [firstName=Ramesh, lastName=Fadatare, userName=Ramesh Fadatare, password=Password, emailId=ramesh@gmail.com, phoneNo=1234567899]
References
https://www.javaguides.net/2020/02/java-scanner-class-example-tutorial.html
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course