Results 1 to 4 of 4
  1. #1
    DanPe is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Help with a Java question (Occurrences of each digit in a string)

    Hi friends,
    I'm trying to answer this question:
    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s)
    The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example,
    after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, counts[3] is 2.
    Write a test program that prompts the user to enter a string and displays the number of occurrences of each digit in the string.

    ** by the way, how do I attach the code so it will be more easy to read in the forum? thanks ;) **
    This is the code I wrote but something is wrong

    Java Code:
    import java.util.Scanner;
    public class Arrays
    {
    	public static void main(String[] args)
    	{
    		Scanner input = new Scanner(System.in);
    		System.out.println("Please enter a string");
    		String str = input.next();
    		isLetterOrDigit(str); //check if digit or letter		
    	}	
    	
    	//check if digit or letter
    	public static void isLetterOrDigit(String str)
    	{
    		String newStr = "";
    		for (int i = 0; i < str.length(); i++)
    		{
    			if (Character.isDigit(str.charAt(i)))
    			{
    				newStr += str.charAt(i);
    			}
    		}
    		count(newStr);
    	}
    	
    	//check occurrences of digits
    	public static int[] count(String newStr)
    	{
    		int[] count = new int[10];
    		
    		for (int i = 0; i < 10; i++)
    		{
    			for (int x = 0; x < newStr.length(); x++)
    			{
    				if (i == newStr.charAt(x))
    				{
    					count[i]++;
    				}
    			}
    			System.out.println("The number " + i + " appears " + count[i] + " times");
    		}
    		return count;
    	}
    }
    Last edited by Fubarable; 05-10-2012 at 11:34 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with a Java question (Occurrences of each digit in a string)

    Quote Originally Posted by DanPe View Post
    Hi friends,
    Hello and welcome to java-forums.org!

    ** by the way, how do I attach the code so it will be more easy to read in the forum? thanks ;) **
    You would surround the code with [code] [/code] tags. I've edited your original question and have added the tags for you.

    This is the code I wrote but something is wrong

    .....
    Can you tell us exactly what is wrong? It helps if we don't have to guess. Post any and all complete error messages, and fully describe any and all misbehaviors.

    Luck and again welcome!

  3. #3
    DanPe is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default Re: Help with a Java question (Occurrences of each digit in a string)

    Hi! thanks for the quick & warm welcome :)
    The problem is somewhere in the count method.
    There are no errors, but I think something is wrong with this part:
    Java Code:
    if (i == newStr.charAt(x))
                    {
                        count[i]++;
                    }
    Is it wrong to use it that way?

    Thanks :)
    Dan

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with a Java question (Occurrences of each digit in a string)

    Yeah, you look to be comparing the char '1' with the int 1, and they're very different entities. For example run this to see what I mean:

    Java Code:
    public class CharToInt {
       public static void main(String[] args) {
          for (char c = '0'; c <= '9'; c++) {
             System.out.println("The char, " + c + ", is represented by the int " + 
                   (int)c);
          }
       }
    }
    If you run it, you'll see this result:
    Java Code:
    The char, 0, is represented by the int 48
    The char, 1, is represented by the int 49
    The char, 2, is represented by the int 50
    The char, 3, is represented by the int 51
    The char, 4, is represented by the int 52
    The char, 5, is represented by the int 53
    The char, 6, is represented by the int 54
    The char, 7, is represented by the int 55
    The char, 8, is represented by the int 56
    The char, 9, is represented by the int 57
    Instead, what you should do is first convert your char to its numeric representation. One way to do this is by simply doing something like this:

    int myInt = myChar - '0';

    Also, there's no need to loop through the numbers 0 through 9. You can use only one for loop that loops through the characters of the String, and then simply use the number obtained above as the index into the array.
    Last edited by Fubarable; 05-11-2012 at 12:14 AM.

Similar Threads

  1. print out occurrences of number in a string
    By katiebear128 in forum New To Java
    Replies: 12
    Last Post: 10-26-2011, 12:36 AM
  2. Replies: 0
    Last Post: 01-23-2011, 01:37 AM
  3. Java String Digit Question
    By toaster_man in forum New To Java
    Replies: 5
    Last Post: 12-12-2010, 10:08 PM
  4. Locating a digit within a string
    By ScentOfAWookie in forum New To Java
    Replies: 8
    Last Post: 03-27-2009, 06:23 PM
  5. Replies: 20
    Last Post: 05-14-2008, 10:42 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
  •