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:
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();
//your work here
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!
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
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.
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);
//your work here
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;
}
}
Re: Counting Sevens in an Integer
Does that compile? If not, and you can't understand the compiler's message, post it.
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.
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);
//your work here
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;
}
}
Re: Counting Sevens in an Integer
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'.
Re: Counting Sevens in an Integer
Apart from all of which you've ignored the hint you posted:
Quote:
Hint: you might consider using integer division or the modulus operator to separate out each digit of n.
db
Re: Counting Sevens in an Integer
Convert it to String and analize it with chatAt(i) method...
Re: Counting Sevens in an Integer
This one works:
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:
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;
}
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:
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
Re: Counting Sevens in an Integer
Interesting algorithm. What do you mean by "rightmost digit"?
Re: Counting Sevens in an Integer