# Thread: Counting Sevens in an Integer

1. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Counting Sevens in an Integer

Hi everyone,
This is the other problem I'm having with my Intro to Programming homework. Any help would be greatly appreciated. I'm completely stuck.

Here's the assignment:

Complete the program below named CountSevens so that it reads in an integer value, counts the number of digits in that number that have the value 7, and prints out the final count. For example, the integer value, 57687728, would have 3 digits with the value 7. Hint: you might consider using integer division or the modulus operator to separate out each digit of n.

Here's my code so far:

Java Code:
```import java.util.Scanner;

/**
Counts the number of digits with value 7 in the decimal
representation of the integer n
*/

public class CountSevens
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter digits: ");
int n = in.nextInt();

System.out.println(countDigits(n));
}

public static int countDigits(int n)
{
int count = 0;

while (int i = 0; i < n.length(); i++)
{
if (Character.isDigit(n.charAt(i)))
{
count++;
}
}
return count;
}
}```
THANK YOU!

2. ## Re: Counting Sevens in an Integer

What is the .length() method for an int supposed to do? hint: primitives (such as ints) don't have methods.

kind regards,

Jos

3. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Counting Sevens in an Integer

Okay, so I see that I should have changed the int to a string. I believe I did that below. I then updated the while loop to look at String instead of int. Found a perhaps more correct way to change to a String. I'm not getting an error on the final "return count;" line.

Java Code:
```import java.util.Scanner;

/**
Counts the number of digits with value 7 in the decimal representation of the integer n
*/

public class CountSevens
{
public static void main(String[] args)
{
String s = "";

Scanner in = new Scanner(System.in);
System.out.print("Enter digits: ");
int n = in.nextInt();
s = Integer.toString(n);

System.out.println(countDigits(s));
}

public static String countDigits(String s)
{
int count = 0;

for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == 7)
{
count++;
}
}
return count;
}
}```
Last edited by jmscarlet9; 03-19-2012 at 11:48 PM.

4. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
16

## Re: Counting Sevens in an Integer

Does that compile? If not, and you can't understand the compiler's message, post it.

5. Member
Join Date
Feb 2012
Posts
57
Rep Power
0

## Re: Counting Sevens in an Integer

I found some more changes I had to make. Right now, it runs without errors but only prints out "0" as the answer.

Java Code:
```import java.util.Scanner;

/**
Counts the number of digits with value 7 in the
decimal representation of the integer n
*/

public class CountSevens
{
public static void main(String[] args)
{
String s = "";
Scanner in = new Scanner(System.in);
System.out.print("Enter digits: ");
int n = in.nextInt();
s = Integer.toString(n);

System.out.println(countDigits(s));
}

public static int countDigits(String s)
{
int count = 0;

for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == 7)
{
count++;
}
}
return count;
}
}```

6. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,717
Rep Power
16

## Re: Counting Sevens in an Integer

Java Code:
`if (s.charAt(i) == 7)`
In Java (and other languages) characters are also regarded as numerical quantities. This can be a bit confusing because there at least three different things that all represent seven in some sense:

7 - this is a number (the fourth prime)
'7' - this is a character (top middle of many keyboards)
"7" - this is a string consisting of a single character

The first two are both numeric quantities but they have different values. In your code you compare the character with the number 7, but you should be using the character '7'.

7. ## Re: Counting Sevens in an Integer

Apart from all of which you've ignored the hint you posted:
Hint: you might consider using integer division or the modulus operator to separate out each digit of n.
db

8. Member
Join Date
Sep 2011
Posts
20
Rep Power
0

## Re: Counting Sevens in an Integer

Convert it to String and analize it with chatAt(i) method...

9. ## Re: Counting Sevens in an Integer

This one works:
Java Code:
```	public static int countSevens (int number)
{
String value = number + "";
int sevenCounter = 0;

for (int i = 0; i < value.length(); i++)
if (value.charAt(i) == '7')
sevenCounter++;

return sevenCounter;
}```
So first do I convert the int argument to a string. Then do I iterate throught the strings character. If the character we are looking at is equal to the character '7' (or, we could write if (value.charAt(i) == 55)), increase the counter by one.
This one is easier to read(its exactly the same as the one above:
Java Code:
```	public static int countSevens (int number)
{
char[] characters = Integer.toString (number).toCharArray();
int sevenCounter = 0;

for (char c : characters)
if (c == '7')
sevenCounter++;

return sevenCounter;
}```
Last edited by Pojahn_M; 03-20-2012 at 04:50 AM.

10. ## Re: Counting Sevens in an Integer

There's no need to convert stuff to a String; the hint in the OP is a giveaway: for a positive number n, n%10 is the rightmost digit and n/10 chops of that digit from the number n. Checking for the number of digits 7 can easly be done recursively:

Java Code:
```int checkSevens(int n) {
if (n == 0) return 0; // the simple case
if (n%10 == 7) // rightmost digit is a 7
return 1+checkSevens(n/10);
return checkSevens(n/10); // else check all but the rigthmost digit
}```
kind regards,

Jos

11. ## Re: Counting Sevens in an Integer

Interesting algorithm. What do you mean by "rightmost digit"?

12. Moderator
Join Date
Apr 2009
Posts
13,540
Rep Power
24

## Re: Counting Sevens in an Integer

Java Code:
```1234567
^```
That one.

#### Posting Permissions

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