Results 1 to 6 of 6
  1. #1
    turnergirl24 is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default Search string for non-alphabetic characters

    Is there an easier way to search a string for non-alphabetic characters than this??

    x = 0;
    while (x != firstName.length()){
    if ( firstName.charAt(x) == 'a' || firstName.charAt(x) == 'b' || firstName.charAt(x) == 'c' || firstName.charAt(x) == 'd' || firstName.charAt(x) == 'e' ||

    firstName.charAt(x) == 'f' || firstName.charAt(x) == 'g' || firstName.charAt(x) == 'h' || firstName.charAt(x) == 'i' || firstName.charAt(x) == 'j' ||

    firstName.charAt(x) == 'k' || firstName.charAt(x) == 'l' || firstName.charAt(x) == 'm' || firstName.charAt(x) == 'n' || firstName.charAt(x) == 'o' ||

    firstName.charAt(x) == 'p' || firstName.charAt(x) == 'q' || firstName.charAt(x) == 'r' || firstName.charAt(x) == 's' || firstName.charAt(x) == 't' ||

    firstName.charAt(x) == 'u' || firstName.charAt(x) == 'v' || firstName.charAt(x) == 'w' || firstName.charAt(x) == 'x' || firstName.charAt(x) == 'y' ||

    firstName.charAt(x) == 'z' || firstName.charAt(x) == 'A' || firstName.charAt(x) == 'B' || firstName.charAt(x) == 'C' || firstName.charAt(x) == 'D' ||

    firstName.charAt(x) == 'E' || firstName.charAt(x) == 'F' || firstName.charAt(x) == 'G' || firstName.charAt(x) == 'H' || firstName.charAt(x) == 'I' ||

    firstName.charAt(x) == 'J' || firstName.charAt(x) == 'K' || firstName.charAt(x) == 'L' || firstName.charAt(x) == 'M' || firstName.charAt(x) == 'N' ||

    firstName.charAt(x) == 'O' || firstName.charAt(x) == 'P' || firstName.charAt(x) == 'Q' || firstName.charAt(x) == 'R' || firstName.charAt(x) == 'S' ||

    firstName.charAt(x) == 'T' || firstName.charAt(x) == 'U' || firstName.charAt(x) == 'V' || firstName.charAt(x) == 'W' || firstName.charAt(x) == 'X' ||

    firstName.charAt(x) == 'Y' || firstName.charAt(x) == 'Z'){
    }else { //
    throw new nonAlphabeticException("Must only be alphabetic characters");

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    yikes!

    Even that won't work. You could use regular expressions to solve this, but I have a feeling that this is for an intro course. Otherwise, you could simply have a testString
    String testString = "abcdefghijklmnopqrstuvwxyz";

    and test to see if each char in your string, changed to lower case, is contained in the testString. See the String API for more details on the String#contains(....) method.

  3. #3
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Java Code:
    String remainingJunk = firstName.replaceAll("[a-zA-Z]","");
    My Hobby Project: LegacyClone

  4. #4
    turnergirl24 is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Thanks for your help!!

  5. #5
    rdtindsm is offline Member
    Join Date
    Feb 2009
    Posts
    92
    Rep Power
    0

    Default

    While checking every character against the contents of the string will work, it's a rather brute force method and horribly inefficient. Consider in the worst case comparing "zzzzzz"
    against "ABCD ..Zabcd...z"). You have to make 52 comparisons for every character.

    An "is_alpha(char unknown_char) is easy to write. In english ascii, characters are coded in sequence [Cap letters], [Numbers and some others],[Low case letters]. IIRC correctly,
    A is X40 and a is X60. You can check that yourself.

    code
    boolean IsAlpha(char a){
    return ((a = 'A' && a <= 'Z') ||
    (a >= 'a' && a = 'z')
    }

    Realizing that we're encouraged to not provide explicitly coded answers, it is so much easier to write the function in code than to explain in english. I think the pedegogy is the same if the OP reads the code. Besides, there are deliberate bugs that are easy to find if the code is understood (edit: and one that wasn't intentional). Can be extended to isAlphaNumeric(), isControlCharacter(), et al. You can revise the function to "IsNotAlpha", or simply use !IsAlpha(a)

    This is also C code and I'm not being careful about using production code. In C, char would probably be an int in production code, but char is a java primitive, so this should be pretty close.

    Anyway, there are only 4 comparisons instead of 52.
    Last edited by rdtindsm; 10-16-2009 at 01:25 AM.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. characters from a string into an integer
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 05-08-2012, 12:31 PM
  2. deleting characters from a String
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 08-29-2008, 12:14 PM
  3. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 08:56 PM
  4. Replies: 0
    Last Post: 11-20-2007, 04:59 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 11:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •