Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: All Digits only

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Post All Digits only

    In this program allDigits(String): accepts a string and returns true if EVERY character is a digit, otherwise false.

    The code seems really simple but i seem not to be getting it here is my code:


    //this should produce a false statement because not all characters are digits
    public class Test {
    public static void main(String[] args) {
    String s = "1were";
    System.out.print(allDigits(s));
    }
    public static boolean allDigits(String s) {
    int sum = 0;
    for (int i = 0; i < s.length(); i++) {
    if( s.charAt(i) > 0 || s.charAt(i) <= 9) {
    return true;
    }
    }
    return false;
    }
    }

    P.S. Im a beginner so let me know if im going in the right direction with this. Thank you


  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: All Digits only

    Just curious but why do you have
    int sum = 0;

    in your boolean method? It doesn't seem like you do anything with it. I'm not sure what your specific error is but I'm pretty sure that it might be your if condition. Just think about what the condition is for a moment.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: All Digits only

    Alright thanks. int sum = 0 was just from another code i forgot to delete it.

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: All Digits only

    You can use the syntax Character.isDigit to determine if a character is a digit or not. Also, in your code, it's possible to exit with 'True' before checking the whole string (if the first character was a digit, it would return True);

    public static boolean allDigits(String s) {
    int sum = 0;
    for (int i = 0; i < s.length(); i++) {
    if (!Character.isDigit(s.charAt(i))) { // if NOT a digit, return false. The ! means "not"
    return false;
    }
    }
    return true; // every character was a digit
    }
    }

  5. The Following User Says Thank You to d9988 For This Useful Post:

    kram (October 28th, 2011)

Similar Threads

  1. Separating Digits in a String
    By Staticity in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2011, 06:39 AM
  2. Problem with digits/letters
    By te09hn8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 22nd, 2011, 07:30 PM
  3. Remove last digits of an IP address.
    By Jonathan_C in forum Algorithms & Recursion
    Replies: 1
    Last Post: November 15th, 2010, 12:55 PM
  4. Replies: 2
    Last Post: February 19th, 2010, 08:10 AM
  5. Replies: 2
    Last Post: February 4th, 2009, 12:24 PM

Tags for this Thread