search
HomeJavajavaTutorialHow to determine username and password in java

How to determine username and password in java

How does Java determine the username and password? Java verification username and password

Assume that a username and password need to be entered in the login interface of a warehouse management system. The username can only consist of 6~10 digits, and the password can only have 6 digits. Any failure to meet the username or password requirements is considered an exception and needs to be caught and handled.

Related video tutorial sharing: java video tutorial

The following uses a custom exception class to complete the verification function of user login information. The implementation steps are as follows.

(1) Write a custom exception class LoginException, which inherits from Exception. The LoginException class contains two constructors, namely the constructor without parameters and the constructor with one parameter. The code is as follows:

public class LoginException extends Exception
{
public LoginException()
{
super();
}
public LoginException(String msg)
{
super(msg);
}
}

(2) Create the test class Test08, in The validateLogin() method is defined in this class to verify username and password. When the username or password does not meet the requirements, use the custom exception class LoginException to output the corresponding exception information. The validateLogin() method is defined as follows:

public boolean validateLogin(String username,String pwd)
{
boolean con=false; //用户名和密码是否正确
boolean conUname=false; //用户名格式是否正确
try
{
if(username.length()>=6&&username.length()<=10)
{
for(int i=0;i<username.length();i++)
{
char ch=username.charAt(i); //获取每一个字符
if(ch>=&#39;0&#39;&&ch<=&#39;9&#39;)
{ //判断字符是否为0~9的数字
conUname=true; //设置 conUname 变量值为 true
}
else
{ //如果字符不是0~9的数字,则拋出LoginException异常
conUname=false;
throw new LoginException("用户名中包含有非数字的字符!");
}
}
}
else
{ //如果用户名长度不在6~10位之间,拋出异常
throw new LoginException("用户名长度必须在6〜10位之间!");
}
if(conUname)
{ //如果用户名格式正确,判断密码长度
if(pwd.length()==6)
{ //如果密码长度等于6
con=true; //设置con变量的值为true,表示登录信息符合要求
}
else
{ //如果密码长度不等于6,拋出异常
con=false;
throw new LoginException("密码长度必须为 6 位!");
}
}
}
catch(LoginException e)
{ //捕获 LoginException 异常
System.out.println(e.getMessage());
}
return con;
}
(3) 在 Test08 类中添加 main() 方法,调用 validateLogin() 方法,如果该方法返回 true,则输出登录成功的信息。main() 方法的定义如下:
 
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("用户名:");
String username=input.next();
System.out.println("密码:");
String password=input.next();
Test08 lt=new Test08 ();
boolean con=lt.validateLogin(username,password); //调用 validateLoginO 方法
if(con)
{
System.out.println("登录成功!");
}
}

In the validateLogin() method of this program, conditional control statements and for loop statements are used to verify the user name and password respectively. Any situation that does not meet the username or password requirements will throw a custom exception LoginException, catch the exception in the catch statement, and output the exception information.

Run the program. When the user name entered by the user contains non-numeric characters, a LoginException exception will be thrown. Execute the code in the catch statement block to print the exception information, as shown below.

用户名:
xiake8!
密码:
123456
用户名中包含有非数字的字符!

When the user name entered by the user is not longer than 6~10 characters, a LoginException exception will also be thrown and the exception information will be printed, as shown below.

用户名:
administrator
密码:
123456
用户名长度必须在6~10位之间!

When the login password entered by the user is not equal to 6 digits, a LogWException exception will also be thrown and the exception information will be printed, as shown below.

用户名:
20181024
密码:
12345
密码长度必须为 6 位!

When the user name and password entered by the user meet the requirements, the login successful information will be printed, as shown below.

用户名:
20181024
密码:
123456
登录成功!

The above is the detailed content of How to determine username and password in java. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does platform independence benefit enterprise-level Java applications?How does platform independence benefit enterprise-level Java applications?May 03, 2025 am 12:23 AM

Java is widely used in enterprise-level applications because of its platform independence. 1) Platform independence is implemented through Java virtual machine (JVM), so that the code can run on any platform that supports Java. 2) It simplifies cross-platform deployment and development processes, providing greater flexibility and scalability. 3) However, it is necessary to pay attention to performance differences and third-party library compatibility and adopt best practices such as using pure Java code and cross-platform testing.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence?May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.Describe a scenario where you encountered a platform-specific issue in Java and how you resolved it.May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

What are the benefits of Java's platform independence for developers?What are the benefits of Java's platform independence for developers?May 03, 2025 am 12:15 AM

Java'splatformindependenceissignificantbecauseitallowsdeveloperstowritecodeonceandrunitonanyplatformwithaJVM.This"writeonce,runanywhere"(WORA)approachoffers:1)Cross-platformcompatibility,enablingdeploymentacrossdifferentOSwithoutissues;2)Re

What are the advantages of using Java for web applications that need to run on different servers?What are the advantages of using Java for web applications that need to run on different servers?May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?How does the JVM contribute to Java's 'write once, run anywhere' (WORA) capability?May 02, 2025 am 12:25 AM

JVM implements the WORA features of Java through bytecode interpretation, platform-independent APIs and dynamic class loading: 1. Bytecode is interpreted as machine code to ensure cross-platform operation; 2. Standard API abstract operating system differences; 3. Classes are loaded dynamically at runtime to ensure consistency.

How do newer versions of Java address platform-specific issues?How do newer versions of Java address platform-specific issues?May 02, 2025 am 12:18 AM

The latest version of Java effectively solves platform-specific problems through JVM optimization, standard library improvements and third-party library support. 1) JVM optimization, such as Java11's ZGC improves garbage collection performance. 2) Standard library improvements, such as Java9's module system reducing platform-related problems. 3) Third-party libraries provide platform-optimized versions, such as OpenCV.

Explain the process of bytecode verification performed by the JVM.Explain the process of bytecode verification performed by the JVM.May 02, 2025 am 12:18 AM

The JVM's bytecode verification process includes four key steps: 1) Check whether the class file format complies with the specifications, 2) Verify the validity and correctness of the bytecode instructions, 3) Perform data flow analysis to ensure type safety, and 4) Balancing the thoroughness and performance of verification. Through these steps, the JVM ensures that only secure, correct bytecode is executed, thereby protecting the integrity and security of the program.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.