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

Threaded View

  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.


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