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: Counting # of 7's in an integer

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Counting # of 7's in an integer

    Hi everyone, I'm in an entry level Java class and I'm supposed to be able to count the # of 7's within an integer. I've read the chapters and looked online for help but can't seem to figure it out. We just learned loops (do, while, for) and if statements so I'm thinking I need to use some of those to solve it. Here's what I came up with so far:

    public class CountSevens 
    {
    public static void main(String[] args) 
    {
        // ask the user for input and assign to a variable
        System.out.print("Please enter an integer: ");
        Scanner in = new Scanner(System.in);
        int number = in.nextInt();
     
        // calculate the number of digits that have a 7
        int count = 0;
        if (number == 0)
        {
            System.out.println("There are no 7's.");
        }
        while (number % 10 == 7)
        {
            count++;
            number = number/10;     
        }
        System.out.println(count);
    }
    }

    Thanks for any help!
    Last edited by greystreet34; October 24th, 2012 at 09:38 PM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Counting # of 7's in an integer

    Does it compile and run? Does it produce the desired results? What set of numbers did you use to test with?

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Counting # of 7's in an integer

    Sorry, the teacher gave us a hint that we might consider using integer division or the modulus operator to separate out each digit of n, then check whether it is a seven. So I'm intending to check whether the ones digit is a 7, then divide by 10 to drop that off and check whether the new ones digit is a 7.

    It does compile, but for some cases I don't get the correct answer.

    For example:

    7 gives me 1.
    0077 gives me 2.
    770 returns nothing.

    It seems when the 7 is not on the right hand side of the integer it doesn't count it correctly.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Counting # of 7's in an integer

    7 gives me 1.
    0077 gives me 2.
    770 returns nothing.

    It seems when the 7 is not on the right hand side of the integer it doesn't count it correctly.
    Now let's consider why... What is the condition in which the body of the loop will be executed?

    It reads like: while the remainder of number divided by ten is seven.
    That is really not what you want to do. What you want to do is check the whole number, every digit, regardless of whether the right most digit is a 7 or not.
    So the loop could read like: while number is greater than ten { do my work to check the right most digit and update my number }
    or: for each digit in the given number { do my work to check the right most digit }

    I would recommend you start with writing down a series of steps to take to solve the problem, and then write code to perform the steps.

Similar Threads

  1. Counting sector from a grid
    By arunk in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 27th, 2012, 06:53 AM
  2. Counting the amount of zeroes, odd and even numbers in an integer
    By 54stickers in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 07:17 PM
  3. Counting cells
    By Shyamz1 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 05:04 PM
  4. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM
  5. Question: counting
    By miss confused in forum Java Theory & Questions
    Replies: 2
    Last Post: July 30th, 2010, 05:38 PM