#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    7
    Rep Power
    0

    How to check if a number contains a certain digit.


    I am new to Java programming language and I want to create a function that checks to see if a number contains a certain digit. For example, I want to check if 587021 contains a 7. The function should return true since it has a 7. I'm trying to use contains() but think it only works for strings.

    This is what i got so far:

    public class Lab2 {
    final static int UNLUCKY_NUM = 7;
    public static boolean hasSeven(int n){
    if (n.contains(UNLUCKY_NUM)){
    return true;
    } else {
    return false;
    }
    }

    public static void main(String[] args) {
    System.out.println(hasSeven(37));
    }
    }

    The if statement has an error.
  2. #2
  3. Walrus Gramps
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2005
    Location
    San Antonio, Texas
    Posts
    1,229
    Rep Power
    619
    Change
    Code:
    if (n.contains(UNLUCKY_NUM)){
    to
    Code:
    if (String.valueOf(n).contains(UNLUCKY_NUM)) {
    Also, please enclose your code in [code] [/code] tags.
    All science is either physics or stamp collecting. - Ernest Rutherford

    How To Ask Questions The Smart Way by Eric S. Raymond
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2006
    Posts
    7
    Rep Power
    0
    Originally Posted by mvantuyl
    Change
    Code:
    if (n.contains(UNLUCKY_NUM)){
    to
    Code:
    if (String.valueOf(n).contains(UNLUCKY_NUM)) {
    Also, please enclose your code in [code] [/code] tags.
    Tried it and still doesn't work. It says "The method contains(CharSequence) in the type String is not applicable for the arguments (int)." I tried changing the int argument to String but no luck either.
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    Originally Posted by manoy385
    Tried it and still doesn't work. It says "The method contains(CharSequence) in the type String is not applicable for the arguments (int)." I tried changing the int argument to String but no luck either.
    There is another method in String that is useful. It is called indexOf. One version of it accepts a String. If the parameter is found in the String that calls the method, then the index where that parameter begins is returned. If it isn't found, then -1 is returned. So you could check the String representation of n to see if the index of the String representation of UNLUCKY_NUM is -1.
    Last edited by bullet; January 24th, 2006 at 11:41 PM.
  8. #5
  9. Feelin' Groovy
    Devshed Supreme Being (6500+ posts)

    Join Date
    Aug 2001
    Location
    WDSMIA
    Posts
    10,135
    Rep Power
    5053
    Java Code:
    String chosenNumber = String.valueOf(n);
    String unluckyNumber = String.valueOf(UNLUCKY_NUM);
    if (chosenNumber.contains(unluckyNumber )) {
        //~ do that voodoo
    }

    I started to write a numeric version of this, but it's more trouble than it's worth...

    Comments on this post

    • mvantuyl agrees
    Last edited by Yawmark; January 25th, 2006 at 07:14 AM.
    Yawmark
    class Sig{public static void main(String...args){\u0066or(int
    \u0020$:"v\"ʲ\"vΤ\"".to\u0043h\u0061rArray()
    )System./*goto/*$/%\u0126//^\u002A\u002Fout.print((char)(($>>
    +(~'"'&'#'))+('<'>>('\\'/'.')/\u002Array.const(~1)\*\u002F)));}}

IMN logo majestic logo threadwatch logo seochat tools logo