Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Question Locating a digit within a string

    Hi,
    I'm trying to locate a single digit within a string containing multiple alphabetical characters. Is it possible to use the Character.isDigit method without having to define a single specific location within the string?
    Alternatively can the indexOf method be used but specifying multiple search definitions instead of a single parameter?
    I'm not supposed to use regular expressions either!
    Cheers
    Last edited by ScentOfAWookie; 03-24-2009 at 10:46 AM.

  2. #2
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    for (int i=0;i<userName.length();i++)
    {
    if (!Character.isDigit(userName.charAt(i)))
    {
    validName=false;
    System.out.println("Your User Name is Invalid.It must contain at least 1 digit!");
    }
    }


    I tried using this. But instead of just checking whether it contains a digit, it actually prints a message for every character that is not
    a digit. Where, as aforementioned, I only want it to print if there's no digit in the entire string!
    Is there something I can insert into this piece of code to rectify this or am I barking up a tree that is in the wrong forest?

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default

    it has to look somethng like this:

    Java Code:
    validName = false;
    start loop
    check to see if string contains digit
    if yes, 
    set variable validName = true
    end loop
    If validName == false
    { print name not valid }
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Question

    for (int counter=0;counter<userName.length();counter++)
    {
    if (Character.isDigit(userName.charAt(counter))==true )
    {
    validName=true;
    }
    else
    {
    validName=false;
    System.out.println("Your User Name is Invalid. It must contain at least one digit!");
    }
    }


    I used this piece of code. My problem is that it prints the "user name is invalid" message for every character that isn't a digit, instead of printing it if the entire string doesn't contain at least one digit. Could somebody tell me where I'm going wrong with this one?

  5. #5
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Sorry I just realised I more or less reposted the original question

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    10

    Default

    ... because you have the println in the loop. Please notice the pseudo code that I posted: the println is out of the loop.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    thanks mate

  8. #8
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Is it possible to do the same with punctuation symbols?
    The user name being entered in my program must contain at least one punctuation symbol/mark.
    The character class doesn't have isPunctuation like it has isDigit and isLetter.
    How would i go about this?

  9. #9
    sayan751 is offline Member
    Join Date
    Feb 2009
    Posts
    29
    Rep Power
    0

    Default

    you can check for the following functions though i am not very much sure.


    static boolean isJavaIdentifierPart(char ch)
    Determines if the specified character may be part of a Java identifier as other than the first character.
    static boolean isJavaIdentifierStart(char ch)
    Determines if the specified character is permissible as the first character in a Java identifier.

    java.sun.com/j2se/1.4.2/docs/api/java/lang/Character.html

Similar Threads

  1. Java newb Q: 8-digit id
    By 1111FM in forum New To Java
    Replies: 2
    Last Post: 09-29-2008, 02:37 PM
  2. convert getValue result in a 4 digit number
    By roseline43 in forum New To Java
    Replies: 0
    Last Post: 09-02-2008, 08:44 PM
  3. Problem locating resources on Linux.
    By jimm1 in forum New To Java
    Replies: 1
    Last Post: 01-18-2008, 07:34 PM
  4. Locating the home interface
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-28-2007, 11:42 AM
  5. problem locating file in web application
    By Rem in forum New To Java
    Replies: 0
    Last Post: 11-09-2007, 05:00 PM

Posting Permissions

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