Fundamentals 8 min read

Java Basics: User Login Validation and Sensitive Word Filtering Examples

This tutorial walks through two Java examples—a simple user login verification program and a basic sensitive‑word filtering utility—explaining the problem setup, code logic, and sample outputs to illustrate input validation and string processing techniques.

Lisa Notes
Lisa Notes
Lisa Notes
Java Basics: User Login Validation and Sensitive Word Filtering Examples

This article presents two practical Java code examples aimed at beginners learning core programming concepts.

Example 1: User Login Validation

The program defines a fixed username ( "[email protected]") and password ( "56789") and reads user input via Scanner. It first checks whether either input is null and prints a prompt before terminating early if so. Then it compares the entered username with the stored one using String.equals(); a mismatch triggers a message indicating an incorrect username. If the username matches, the password is compared similarly; a mismatch results in a password‑error message. When both match, the code extracts the part before the '@' symbol from the stored email, builds a welcome string, and prints it. The article notes that in real applications credentials would be retrieved from a database or file rather than hard‑coded.

import java.util.Scanner;
public class UserLogin {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String mailbox = "[email protected]";
        String password = "56789";
        String userName, userPwd;
        userName = scanner.next().trim();
        userPwd = scanner.next().trim();
        if (userName == null || userPwd == null) {
            System.out.println("请输入用户名和密码!");
            return;
        }
        if (!userName.equals(mailbox)) {
            System.out.println("抱歉,您的用户名不正确!请重新输入");
        } else if (!userPwd.equals(password)) {
            System.out.println("抱歉,您的密码不正确!请重新输入");
        } else {
            int index = mailbox.indexOf('@');
            String name = mailbox.substring(index + 1);
            System.out.println(name + ",欢迎您!您已经通过验证,可以进行操作!");
        }
    }
}

Running the program with the correct credentials produces the output:

[email protected]
56789
wanglaoshi,欢迎您!您已经通过验证,可以进行操作!

Example 2: Sensitive Word Filtering

The second program demonstrates how to filter and replace sensitive words in a user‑provided string. Two string arrays are defined: arr1 holds words to be removed entirely, and arr2 holds words to be replaced with the word "文明". The program reads a line of text from the console, checks that input exists, then iterates over arr1 to delete any occurrence using String.replaceAll(). A second loop processes arr2, replacing each found word with "文明". Finally, the filtered result is printed.

import java.util.Scanner;
/**
 * 敏感词过滤
 */
public class SensitiveWordFiltration {
    public static void main(String[] args) {
        String[] arr1 = {"山寨", "敏感", "gta"}; // words to block
        String[] arr2 = {"暴力", "毒品", "赌博"}; // words to replace
        Scanner sca = new Scanner(System.in);
        String[] arr3 = new String[3];
        arr3[0] = sca.next().trim();
        if (arr3.length < 1) {
            System.out.println("没有输入聊天内容,请重新输入!");
            return;
        }
        String s = arr3[0];
        for (int i = 0; i < arr1.length; i++) {
            int index = s.indexOf(arr1[i]);
            if (index != -1) {
                s = s.replaceAll(arr1[i], "");
            }
        }
        for (int i = 0; i < arr2.length; i++) {
            int index = s.indexOf(arr2[i]);
            if (index != -1) {
                s = s.replaceAll(arr2[i], "文明");
            }
        }
        System.out.println("您的聊天内容为:" + s);
    }
}

Given the input "我喜欢山寨,我喜欢赌博", the program outputs:

您的聊天内容为:我喜欢,我喜欢文明

The article emphasizes that in production code the arrays of sensitive words would typically be loaded from a database or external file rather than being hard‑coded.

Both examples illustrate fundamental Java techniques such as console input handling, string manipulation, conditional logic, and early termination, providing concrete, runnable code for learners to experiment with.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaInput ValidationProgramming TutorialSensitive Word FilteringUser Login
Lisa Notes
Written by

Lisa Notes

Lisa's notes: musings on daily life, work, study, personal growth, and casual reflections.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.