Results 1 to 12 of 12
Thread: Counting Sevens in an Integer
 03192012, 08:59 PM #1Member
 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(); //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; } }
 03192012, 09:05 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,436
 Blog Entries
 7
 Rep Power
 20
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,
Joscenosillicaphobia: the fear for an empty beer glass
 03192012, 10:24 PM #3Member
 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); //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; } }
Last edited by jmscarlet9; 03192012 at 10:48 PM.
 03192012, 10:51 PM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Re: Counting Sevens in an Integer
Does that compile? If not, and you can't understand the compiler's message, post it.
 03192012, 11:07 PM #5Member
 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); //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; } }
 03202012, 12:42 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Re: Counting Sevens in an Integer
Java Code:if (s.charAt(i) == 7)
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'.
 03202012, 12:48 AM #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.If you're forever cleaning cobwebs, it's time to get rid of the spiders.
 03202012, 01:06 AM #8Member
 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...
 03202012, 03:45 AM #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; }
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; 03202012 at 03:50 AM.
 03202012, 08:28 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,436
 Blog Entries
 7
 Rep Power
 20
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 }
Joscenosillicaphobia: the fear for an empty beer glass
 03202012, 03:44 PM #11
Re: Counting Sevens in an Integer
Interesting algorithm. What do you mean by "rightmost digit"?
 03202012, 04:01 PM #12Moderator
 Join Date
 Apr 2009
 Posts
 11,949
 Rep Power
 19
Similar Threads

Integer Comparison, Outputting Largest Integer Not Working
By killingthemonkey in forum New To JavaReplies: 4Last Post: 10162011, 08:59 PM 
Array for counting from 1 to 100
By Magic8ballfollower in forum New To JavaReplies: 5Last Post: 09222011, 04:55 AM 
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05172010, 12:50 AM 
Counting digits in an integer value, including zero
By lithium002 in forum New To JavaReplies: 1Last Post: 12042009, 04:56 AM 
Counting help
By jksmithson in forum New To JavaReplies: 1Last Post: 11062009, 02:43 AM
Bookmarks