Results 1 to 7 of 7
  1. #1
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

    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:

    Java Code:
    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 advice!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

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

    So what is the problem? Does the code compile? If not and you can't understand the compiler's messages, post them.

    Or does it compile, but not do what you expect or intend when you run it? In that case describe what it *does* do - and if that is to throw a runtime exception, post the stack trace.

    ---

    It might be a good idea if you described how you are going about counting the sevens. Just in plain language, I mean, not code. I say this because the condition in the while statement looks a little odd. "while (number % 10 == 7)" means something like "while number ends in a seven" and its hard to see what's intended there.

  3. #3
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

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

    I took a look at your code and ran it using the test number 7177, which reported 2 7s. The reason for this, as pbrockway2 mentioned at the end of the post, your program basicly looks at the last digit and reports if there is a 7. That is fine until it hits the 1, at which point (number % 10 == 7) becomes false and the while loop is done. You are very close, I only had to change one line, add one if statement, and move one line down a line to add a }.
    Try doing a debug and watching to see what number is when it gets to the System.out.println(count); line, you will find that number=71. As I mentioned above, number%10 would equal 1. What does that do to your while loop? One of the keys here is to fix the while loop. In the test, what is true about 7177 and 71 but becomes different after the run when number= 7?
    Hint: ROT13: ahzore zbq 10 orpbzrf 0. Fb vafgrnq eha gur ybbc nf ybat nf ahzore vf terngre guna 0. (Lbh pna abj nyfb gnxr bhg gur svefg vs fgngrzrag). Jung lbh pheeragyl unir va gur juvyr grfg, chg gung va na vs fgngrzrag.

    Good luck.
    PS. ROT 13 means the characters are translated or slid 13 places in the alphabet. You can find translators online, though mine done by remaping my keyboard in the OS (Mac)

    PPS. I was going to attach an image of the code I changed but I didnt want to make it too easy you will need a mirror to read it. After I put in the hint I was going to remove it unless you needed more help but it won't let me.
    Attached Thumbnails Attached Thumbnails Counting # of 7's in an integer-screen-shot-2012-10-25-3.34.36-am.png  
    Last edited by traisjames; 10-25-2012 at 10:58 AM.

  4. #4
    greystreet34 is offline Member
    Join Date
    Oct 2012
    Posts
    8
    Rep Power
    0

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

    Thank you very much! It makes a lot more sense after you explained it. Also, never heard of ROT 13 before ohg vg'f cerggl pbby!

  5. #5
    traisjames is offline Member
    Join Date
    Oct 2012
    Location
    NE Iowa
    Posts
    11
    Rep Power
    0

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

    ROT 13 is great for when you want to give an explination but dont want to just give it away. Many fourms which may have spoilers include a ROT13 converter

  6. #6
    sjicho is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

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

    try

    import java.util.Scanner;

    public class Random {
    public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Number: ");
    int intInputNUmber = scanner.nextInt();

    String strNumber = String.valueOf(intInputNUmber);
    char chrComparison = '7';
    int intNumberOfSevens = 0;

    for(int i = 0;i < strNumber.length();i++) {
    char chrTemp = strNumber.charAt(i);
    if(chrComparison == chrTemp) {
    intNumberOfSevens++;
    }
    }

    System.out.println("Number of sevens: " + intNumberOfSevens);
    }
    }

    and my question is how do you print code like that on the forum

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,419
    Rep Power
    20

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

    Quote Originally Posted by sjicho View Post
    try

    import java.util.Scanner;

    public class Random {
    public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Number: ");
    int intInputNUmber = scanner.nextInt();

    String strNumber = String.valueOf(intInputNUmber);
    char chrComparison = '7';
    int intNumberOfSevens = 0;

    for(int i = 0;i < strNumber.length();i++) {
    char chrTemp = strNumber.charAt(i);
    if(chrComparison == chrTemp) {
    intNumberOfSevens++;
    }
    }

    System.out.println("Number of sevens: " + intNumberOfSevens);
    }
    }

    and my question is how do you print code like that on the forum
    Quote Originally Posted by sjicho View Post
    try

    import java.util.Scanner;

    public class Random {
    public static void main(String args[]) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Number of integers: ");
    int intNumberOfIntegers = scanner.nextInt();

    int intStorage[] = new int[intNumberOfIntegers];

    System.out.println("Please prompt numbers!");

    for(int i = 0;i < intStorage.length;i++) {
    intStorage[i] = scanner.nextInt();
    }

    System.out.println("Your numbers are");

    for(int i = 0;i < intStorage.length;i++) {
    System.out.print(intStorage[i] + " ");
    }
    }
    }
    1. Don't spoonfeed. With the guidance already given, the OP could have figured out the code -- or asked for clarification. A forum is a place to learn, not a free code handout center.

    2. Go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    3. It's bad practise (a) to name a class after an existing JDK class and (b) to use a meaningless class name that doesn't reflect its purpose.

    4. You already asked the question you appended, and I answered it on How to post code

    Any further spoonfeeding will be deleted and you may face a ban. We're serious about helping people to learn.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Counting Sevens in an Integer
    By jmscarlet9 in forum New To Java
    Replies: 11
    Last Post: 03-20-2012, 04:01 PM
  2. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 08:59 PM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. Counting digits in an integer value, including zero
    By lithium002 in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 04:56 AM
  5. Counting help
    By jksmithson in forum New To Java
    Replies: 1
    Last Post: 11-06-2009, 02:43 AM

Posting Permissions

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