Home > DeveloperSection > Forums > Read Special Character in java Strings
Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    December-29-2015 1:30 AM

 Java Java  String 
Ratings:


 1 Reply(s)
 315  View(s)
Rate this:
I want to test wheather my input string conatins any special characters or not. Is there any good techniques for impelmting this?

I am using charAt() and ASCII values
for(int i=0;i<s.length();i++)
{
    char c=s.charAt(i);
    if(65<=c<=90)
    {
        System.out.println("Valid");
    }
    else if(97<=c<=122)
    {
        System.out.println("Invalid as it contains lower case letter");
        break;
    }
    else if(48<=c<=57)
    {
        System.out.println("Invalid as it contains numbers");
        break;
    }
    else
        System.out.println("Invalid as it contains special characters");
}


Mayank Tripathi
Mayank Tripathi

Total Post:397

Points:3117
Posted on    December-29-2015 7:15 AM

The problem you have is that something like this: 65<=c<=90 will not work in java. You can't chain compares!

You'll have to write something like this:

if (65 <= c && c <= 90)
Or to increase readability i'd suggest you to do something like this:

if ('A' <= c && c <= 'Z')

Don't want to miss updates? Please click the below button!

Follow MindStick