Results 1 to 12 of 12
  1. #1
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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;
      }
    }
    THANK YOU!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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; 03-19-2012 at 10:48 PM.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Counting Sevens in an Integer

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

  5. #5
    jmscarlet9 is offline Member
    Join Date
    Feb 2012
    Posts
    57
    Rep Power
    0

    Default 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;
    	}
    }

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    DIego912 is offline Member
    Join Date
    Sep 2011
    Posts
    20
    Rep Power
    0

    Default Re: Counting Sevens in an Integer

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

  9. #9
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default 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 03:50 AM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,360
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Pojahn_M's Avatar
    Pojahn_M is offline Senior Member
    Join Date
    Mar 2011
    Location
    Sweden
    Posts
    197
    Rep Power
    4

    Default Re: Counting Sevens in an Integer

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

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default Re: Counting Sevens in an Integer

    Java Code:
    1234567
          ^
    That one.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Integer Comparison, Outputting Largest Integer Not Working
    By killingthemonkey in forum New To Java
    Replies: 4
    Last Post: 10-16-2011, 08:59 PM
  2. Array for counting from 1 to 100
    By Magic8ballfollower in forum New To Java
    Replies: 5
    Last Post: 09-22-2011, 04:55 AM
  3. convert unsigned integer to signed integer in java?
    By diskhub in forum New To Java
    Replies: 6
    Last Post: 05-17-2010, 12:50 AM
  4. Counting digits in an integer value, including zero
    By lithium002 in forum New To Java
    Replies: 1
    Last Post: 12-04-2009, 04:56 AM
  5. Counting help
    By jksmithson in forum New To Java
    Replies: 1
    Last Post: 11-06-2009, 02:43 AM

Posting Permissions

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