Java 如何检查字符串,以便只允许使用字母和$符号?

Java 如何检查字符串,以便只允许使用字母和$符号?,java,Java,在NormalizeDiscoveryCode中,验证是否仅使用字母或$字符。如果使用了任何其他字符,则抛出带有消息“折扣代码无效”的IllegalArgumentException 我试过这个 char[] chars = code.toCharArray(); if (! Character.isLetter(chars.length) || code != "$"){ //if (code.matches("[a-zA-Z$]")){ throw new IllegalAr

在NormalizeDiscoveryCode中,验证是否仅使用字母或$字符。如果使用了任何其他字符,则抛出带有消息“折扣代码无效”的IllegalArgumentException

我试过这个

char[] chars = code.toCharArray();
if (! Character.isLetter(chars.length) || code != "$"){
    //if (code.matches("[a-zA-Z$]")){
    throw new IllegalArgumentException("Invalid discount code");
}

this.discountCode = code.toUpperCase();
return code.toUpperCase();
}
还有这个

if (! Character.isLetter(code.length()) || code != "$"){
throw new IllegalArgumentException("Invalid discount code");
}

this.discountCode = code.toUpperCase();
return code.toUpperCase();
}

public class Order {
private String itemName;
private int priceInCents;
private String discountCode;

private String normalizeDiscountCode(String code){
    //char[] chars = code.toCharArray();
    if (! Character.isLetter(code.length()) || code != "$"){
        //if (code.matches("[a-zA-Z$]")){
        throw new IllegalArgumentException("Invalid discount code");
    }

    this.discountCode = code.toUpperCase();
    return code.toUpperCase();
}

public Order(String itemName, int priceInCents) {
    this.itemName = itemName;
    this.priceInCents = priceInCents;
}

public String getItemName() {
    return itemName;
}

public int getPriceInCents() {
    return priceInCents;
}

public String getDiscountCode() {
    return discountCode;
}

public void applyDiscountCode(String discountCode) {
    this.discountCode = discountCode;
    this.discountCode = normalizeDiscountCode(discountCode);
}
}

public class Order {
private String itemName;
private int priceInCents;
private String discountCode;

private String normalizeDiscountCode(String code){
    //char[] chars = code.toCharArray();
    if (! Character.isLetter(code.length()) || code != "$"){
        //if (code.matches("[a-zA-Z$]")){
        throw new IllegalArgumentException("Invalid discount code");
    }

    this.discountCode = code.toUpperCase();
    return code.toUpperCase();
}

public Order(String itemName, int priceInCents) {
    this.itemName = itemName;
    this.priceInCents = priceInCents;
}

public String getItemName() {
    return itemName;
}

public int getPriceInCents() {
    return priceInCents;
}

public String getDiscountCode() {
    return discountCode;
}

public void applyDiscountCode(String discountCode) {
    this.discountCode = discountCode;
    this.discountCode = normalizeDiscountCode(discountCode);
}
}

需要使用foreach循环,这是我一直在努力解决的问题。我很难弄清楚如何将完整字符串转换成字符

private String normalizeDiscountCode(String code){
    for (int i = 0; i < code.length(); i++){
        char c = code.charAt(i);
        if (! Character.isLetter(c) && c != '$'){
            throw new IllegalArgumentException("Invalid discount code");
        }
    }
    this.discountCode = code.toUpperCase();
    return code.toUpperCase();
}
私有字符串规范化搜索代码(字符串代码){
对于(int i=0;i
您的正则表达式模式只匹配一个字符。要匹配一个或多个,可以使用
[a-zA-Z$]+
。因此您尝试了
code.matches(“[a-zA-Z$]”)
,但是没有考虑在那里添加一个量词来使其工作?您的条件是错误的。如果字符不是字母或不是
$
,则抛出异常。当然,字母不是
$
$
也不是字母。它应该是
&&
,而不是
|
。另外,为什么要检查数组的长度?长度不是字母。您应该检查每个字符。在这种情况下,我是否必须为每个循环使用a?