You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用String.contains()检测字符串特殊字符的Java程序异常问题

Java字符串特殊字符检测问题修复

问题根源

你的代码核心错误在于answer.contains(SimpleSymbol)——这个方法是检查输入字符串是否完整包含整个特殊符号串(即!@#$%^&*()_+这一整串),而非包含其中任意一个特殊字符,这就是输入he!!o未被识别的原因。另外代码还有几处小问题:

  • 逻辑倒置:你要求包含特殊字符的字符串加入arrayList1,但代码里却加入了arrayList2
  • 输出语句中的password变量未定义,应替换为answer
  • 注释掉的do{没有闭合,若需要循环输入需补全循环结构

修复方案

方案1:遍历特殊字符逐一检查

将特殊符号串转为字符数组,遍历每个字符判断输入字符串是否包含:

import java.util.ArrayList;
import java.util.Scanner;

public class PasswordChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String answer;
        ArrayList<String> arrayList1 = new ArrayList<>(); // 存储含特殊字符的字符串
        ArrayList<String> arrayList2 = new ArrayList<>(); // 存储不含特殊字符的字符串
        String simpleSymbols = "!@#$%^&*()_+";
        
        do {
            System.out.println("enter passwords");
            answer = input.nextLine();
            boolean hasSpecialChar = false;
            
            // 遍历每个特殊字符做检测
            for (char c : simpleSymbols.toCharArray()) {
                if (answer.contains(String.valueOf(c))) {
                    hasSpecialChar = true;
                    break;
                }
            }
            
            if (hasSpecialChar) {
                arrayList1.add(answer);
                System.out.println("含特殊字符: " + arrayList1);
            } else {
                arrayList2.add(answer);
                System.out.println("不含特殊字符: " + arrayList2);
            }
            
            // 可选:询问是否继续输入
            System.out.println("继续输入?(y/n)");
            String continueInput = input.nextLine();
            if (!continueInput.equalsIgnoreCase("y")) {
                break;
            }
        } while (true);
        
        input.close();
    }
}

方案2:使用正则表达式(更简洁)

通过正则表达式匹配任意指定特殊字符:

import java.util.ArrayList;
import java.util.Scanner;

public class PasswordChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String answer;
        ArrayList<String> arrayList1 = new ArrayList<>();
        ArrayList<String> arrayList2 = new ArrayList<>();
        // 正则规则:匹配括号内任意一个特殊字符
        String specialCharRegex = "[!@#$%^&*()_+]";
        
        do {
            System.out.println("enter passwords");
            answer = input.nextLine();
            
            // .* 匹配任意字符(0次或多次),确保特殊字符出现在字符串任意位置都能被检测
            if (answer.matches(".*" + specialCharRegex + ".*")) {
                arrayList1.add(answer);
                System.out.println("含特殊字符: " + arrayList1);
            } else {
                arrayList2.add(answer);
                System.out.println("不含特殊字符: " + arrayList2);
            }
            
            System.out.println("继续输入?(y/n)");
            String continueInput = input.nextLine();
            if (!continueInput.equalsIgnoreCase("y")) {
                break;
            }
        } while (true);
        
        input.close();
    }
}

关键说明

  • contains(CharSequence s)方法仅检测字符串是否包含连续的子串,而非单个字符
  • 正则表达式中[!@#$%^&*()_+]表示匹配括号内的任意一个字符,.*用于覆盖特殊字符在字符串中的任意位置
  • 程序结束前记得关闭Scanner资源,避免内存泄漏

内容的提问来源于stack exchange,提问作者IS THIS TWITTER

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:21:26