Tooilia
Tooilia

Reputation: 39

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. 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

Upvotes: 2

Views: 10652

Answers (4)

Ted Hopp
Ted Hopp

Reputation: 234847

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).

If the test is going to be done many times (which isn't the case in your posted code), it would be more efficient to pre-compile the pattern:

private static final Pattern p = Pattern.compile("^[$%]*$");

public static boolean isValidCode (String code) {
    return p.matcher(code).matches();
}

(The call to code.matches(...) is just a convenience method does the above.)

Upvotes: 6

user1675825
user1675825

Reputation:

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;
            }
        }
    }
}

Upvotes: 0

Random42
Random42

Reputation: 9159

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); * say that it must appear 0 or more times.

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

Use Character Class.

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$");
    }


     }



}

Upvotes: 0

Related Questions