Results 1 to 7 of 7
Thread: Counting # of 7's in an integer
- 10-25-2012, 05:40 AM #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:
Thanks for any advice!Java Code:public class CountSevens { public static void main(String[] args) { // ask the user for input and assign to a variable System.out.print("Please enter an integer: "); 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); } }
- 10-25-2012, 06:31 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 10-25-2012, 10:56 AM #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.
- 10-26-2012, 01:59 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!
- 10-26-2012, 02:19 AM #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
- 10-26-2012, 01:13 PM #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
- 10-26-2012, 01:44 PM #7
Re: Counting # of 7's in an integer
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 Guide For New Members 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.
4. You already asked the question you appended, and I answered it on How to post code
Any further spoonfeeding will be deleted and you may face a ban. We're serious about helping people to learn.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Counting Sevens in an Integer
By jmscarlet9 in forum New To JavaReplies: 11Last Post: 03-20-2012, 04:01 PM -
Integer Comparison, Outputting Largest Integer Not Working
By killingthemonkey in forum New To JavaReplies: 4Last Post: 10-16-2011, 08:59 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
Counting digits in an integer value, including zero
By lithium002 in forum New To JavaReplies: 1Last Post: 12-04-2009, 04:56 AM -
Counting help
By jksmithson in forum New To JavaReplies: 1Last Post: 11-06-2009, 02:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks