Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default Can somebody check my codes?(Counting numbers)

    This program is about counting the number of digits that exist in a string
    there is no compile error on it but there is an error in the logics that i cant find it. So can somebody have a look at it, if you cant tell me the direct answer then please give me some hints. Thanks

    Here is my codes
    Java Code:
    public class OccurrenceString
          {
                public static void main (String[] args)
                {
                                         System.out.println("SSN is 343 32 4545 and ID is 434 34 4323");
    
    				String SSNandID = "SSN is 343 32 4545 and ID is 434 34 4323";
    
    				int[] countNum = count(SSNandID);
    
    			
    
    				//display count array
    				for(int i=0; i < countNum.length; i++)
    				{
    					if(countNum[i] != 0)
                        System.out.println(i +" appears "+ countNum[i] + ((countNum[i]==1)?" times.":" times."));
    				}
    			}//end of main
    
    	public static int[] count(String SSNandID)
    	{
    		int counts[] = new int[10];
    
    		for(int i = 0; i< SSNandID.length(); i++)
    		{
    			if(Character.isDigit(SSNandID.charAt(i)))
    			{
    
    			counts[SSNandID.charAt(i)]++;
    		    }
    		}//end of for
    		return counts;
    
    	}//end of count method
    
    	}//end of OccurrenceString
    Last edited by PureAwesomeness; 07-01-2009 at 04:26 AM.

  2. #2
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    This line might cause arrayIndexOutofBoundsExeption
    Java Code:
    public static int[] count(String SSNandID)
    	{
    		[COLOR="Red"]int counts[] = new int[10];[/COLOR]
    try this:

    Java Code:
    public static int[] count(String SSNandID)
    	{
    		[COLOR="Red"]int counts[] = new int[SSNandID.length()];[/COLOR]
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  3. #3
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by azzaiel View Post
    This line might cause arrayIndexOutofBoundsExeption
    Java Code:
    public static int[] count(String SSNandID)
    	{
    		[COLOR="Red"]int counts[] = new int[10];[/COLOR]
    try this:

    Java Code:
    public static int[] count(String SSNandID)
    	{
    		[COLOR="Red"]int counts[] = new int[SSNandID.length()];[/COLOR]

    i cant use that because that is the length of the string. i used 10 in that array becuase there i wanted the array starts from 0-9 and increment a number whenever a number exists in the SSNandID string. its like counting how many 1s,2s,3s...9s in that string.
    Thank you for your respond

  4. #4
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    ok sorry

    Java Code:
    public static int[] count(String SSNandID)
    	{
    		int counts[] = new int[10];
    
    		for(int i = 0; i< SSNandID.length(); i++)
    		{
    			if(Character.isDigit(SSNandID.charAt(i)))
    			{
    
    			counts[i]++;
    		    }
    		}//end of for
    		return counts;
    
    	}//end of count method

    change that to this:

    Java Code:
    public static int[] count(String SSNandID) {
    		int counts[] = new int[11];
    		
    		for (int i = 0; i < SSNandID.length(); i++) {
    			
    			if (Character.isDigit(SSNandID.charAt(i))) {
    				final Character cNum = SSNandID.charAt(i);
    				final String sNum = cNum.toString();
    			    int num = Integer.parseInt(sNum);
    			    counts[num] += 1;
    			}
    		}// end of for
    		return counts;
    
    	}
    tell me if you have questions
    Last edited by azzaiel; 07-01-2009 at 05:25 AM.
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    @OP: Much similar to the switch-case logic. If you can start from the basis logic, easy to find the solution. ;)

    Just adding more..

    Following is the switch-case logic

    Java Code:
                    switch(SSNandID.charAt(i)) {
                        case '0':
                            counts[0]++;
                            break;
                        case '1':
                            counts[1]++;
                            break;
    No compare it with azzaiels' code. Doing the same thing in much much better way. :)
    Last edited by Eranga; 07-01-2009 at 04:34 AM. Reason: Adding more...

  6. #6
    Join Date
    Jan 2009
    Posts
    90
    Rep Power
    0

    Default

    Thanks guys i got it!!!

  7. #7
    azzaiel's Avatar
    azzaiel is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    @Eranga

    so your gona make a case from case'0' to case'10' and hard code the value of the index of the array hmmm hows that better?
    It's easy to write a code that computers can understand...
    ... the challenge is to write a code that humans can understand

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by azzaiel View Post
    @Eranga

    so your gona make a case from case'0' to case'10' and hard code the value of the index of the array hmmm hows that better?
    No way, that's the bad way I can program the logic. What I'm trying to point there is, that's the basic logic and implementation we can follow. Very basis about the deal with multiple options.

    I never advice to follow that way in any case.

  9. #9
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    My humble 2c's worth...

    Java Code:
    package forums;
    
    class FrequencyOfDigitsTest
    {
      public static void main(String[] args) {
        try {
          System.out.println(java.util.Arrays.toString(frequencyOfDigits("0123456789012345")));
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
      public static int[] frequencyOfDigits(String text) {
        int counts[] = new int[10];
        for ( char c : text.toCharArray() ) {
          if ( Character.isDigit(c) ) {
            counts[Integer.parseInt(String.valueOf(c))]++;
          }
        }
        return counts;
      }
    }

Similar Threads

  1. Counting numbers up and down
    By radio in forum New To Java
    Replies: 4
    Last Post: 05-06-2011, 03:03 PM
  2. Recursive Counting
    By zlwilly in forum New To Java
    Replies: 1
    Last Post: 01-29-2009, 08:42 PM
  3. Counting characters
    By Tiff89 in forum New To Java
    Replies: 10
    Last Post: 12-12-2008, 09:21 AM
  4. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  5. Counting Pixels
    By shaungoater in forum Java 2D
    Replies: 5
    Last Post: 11-29-2007, 05:51 PM

Posting Permissions

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