# Thread: Counting # of 7's in an integer

1. Member
Join Date
Oct 2012
Posts
8
Rep Power
0

## 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
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);
}
}```

2. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15

## 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. Member
Join Date
Oct 2012
Location
NE Iowa
Posts
11
Rep Power
0

## 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.
Last edited by traisjames; 10-25-2012 at 10:58 AM.

4. Member
Join Date
Oct 2012
Posts
8
Rep Power
0

## 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. Member
Join Date
Oct 2012
Location
NE Iowa
Posts
11
Rep Power
0

## 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. Member
Join Date
Oct 2012
Posts
4
Rep Power
0

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

Originally Posted by sjicho
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
Originally Posted by sjicho
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];

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

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 http://www.java-forums.org/forum-gui...w-members.html 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.

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

db

#### Posting Permissions

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