简体   繁体   English

字符串仅接受某些字符

[英]String only accepts certain characters

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        System.out.println("Enter Code");

        String code = input.next();
    }

    public static boolean isValidCode (String code) {

    }
}

I'm having a lot of trouble in java when I try to make restrictions on input. 当我尝试限制输入时,我在Java中遇到了很多麻烦。 In this example, I need the string code to only accept symbols such as $ and % . 在此示例中,我需要字符串代码仅接受$%符号。 How can I check to make sure there are no numbers,letters, or other symbols? 如何检查以确保没有数字,字母或其他符号? Thanks 谢谢

This should work to invalidate strings that contain anything except $ and/or % : 这应该使包含$和/或%以外的任何内容的字符串无效:

public static boolean isValidCode (String code) {
    return code.matches("^[$%]*$");
}

If you also require that the string not be empty, then change the * to a + . 如果还要求字符串不为空,则将*更改为+ If you need to accept other characters, just add them to the character class list (the characters between the square brackets). 如果需要接受其他字符,只需将它们添加到字符类列表(方括号之间的字符)即可。

public static boolean isValidCode(String code) {
    return code.matches("[$%]*");
}

As you can see in regex javadoc, the angle brackets say that you can choose between the enclosing characters ($ and % in your case); 如您在regex javadoc中所看到的,尖括号表示您可以在封闭的字符(在您的情况下为$和%)之间进行选择。 * say that it must appear 0 or more times. *说必须出现0次或多次。

Use Character Class. 使用Character类。

Here is the Code for you : 这是为您提供的代码:

public class Hello {




    public static void main(String[] args) {


    Scanner input = new Scanner(System.in);
    System.out.println("Enter Code");

     String code = input.next();
     char c = code.charAt(0);
    if( (!Character.isDigit(c)) && (!Character.isLetter(c) && (!Character.isWhitespace(c)))){

        if(c == '%' || c=='$'){

            System.out.println("Its what u want");
        }else{
            System.out.println("Not what u want");
        }
    }else{
        System.out.println("Not what u want$");
    }


     }



}
import java.util.Scanner;

public class Test 
{
    public static void main(String[] args) 
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter Code");

        String code = input.next();
        for(int i=0;i<code.length();i++)
        {
            char c = code.charAt(i);

            if((!Character.isDigit(c))&&(!Character.isLetter(c)&&(!Character.isWhitespace(c))))      
            {
                if(c=='%'||c=='$')
                {
                    //What U Want..........
                }
                else
                {
                    System.out.println("Plz! Enter Only '%' or '$'");
                    break;
                }
            }//if
            else
            {
                System.out.println("Allows Only '%' or '$' Symbols");
                break;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM