Results 1 to 12 of 12
Thread: Counting Sevens in an Integer
 03192012, 09: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, 10:05 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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,
JosBuild a wall around Donald Trump; I'll pay for it.
 03192012, 11: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 11:48 PM.
 03192012, 11:51 PM #4Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
Re: Counting Sevens in an Integer
Does that compile? If not, and you can't understand the compiler's message, post it.
 03202012, 12:07 AM #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, 01:42 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 15
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, 01: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, 02: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, 04: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 04:50 AM.
 03202012, 09:28 AM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,414
 Blog Entries
 7
 Rep Power
 25
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 }
JosBuild a wall around Donald Trump; I'll pay for it.
 03202012, 04:44 PM #11
Re: Counting Sevens in an Integer
Interesting algorithm. What do you mean by "rightmost digit"?
 03202012, 05:01 PM #12Moderator
 Join Date
 Apr 2009
 Posts
 13,378
 Rep Power
 24
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, 05:56 AM 
Counting help
By jksmithson in forum New To JavaReplies: 1Last Post: 11062009, 03:43 AM
Bookmarks