Results 1 to 9 of 9
- 07-01-2009, 03:29 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
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 OccurrenceStringLast edited by PureAwesomeness; 07-01-2009 at 04:26 AM.
- 07-01-2009, 03:55 AM #2
This line might cause arrayIndexOutofBoundsExeption
try this:Java Code:public static int[] count(String SSNandID) { [COLOR="Red"]int counts[] = new int[10];[/COLOR]
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
- 07-01-2009, 04:06 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
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
- 07-01-2009, 04:24 AM #4
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:
tell me if you have questionsJava 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; }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
- 07-01-2009, 04:30 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
@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
No compare it with azzaiels' code. Doing the same thing in much much better way. :)Java Code:switch(SSNandID.charAt(i)) { case '0': counts[0]++; break; case '1': counts[1]++; break;Last edited by Eranga; 07-01-2009 at 04:34 AM. Reason: Adding more...
- 07-01-2009, 05:11 AM #6
Member
- Join Date
- Jan 2009
- Posts
- 90
- Rep Power
- 0
Thanks guys i got it!!!
- 07-01-2009, 05:28 AM #7
@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
- 07-01-2009, 02:12 PM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 07-01-2009, 03:03 PM #9
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
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
-
Counting numbers up and down
By radio in forum New To JavaReplies: 4Last Post: 05-06-2011, 03:03 PM -
Recursive Counting
By zlwilly in forum New To JavaReplies: 1Last Post: 01-29-2009, 08:42 PM -
Counting characters
By Tiff89 in forum New To JavaReplies: 10Last Post: 12-12-2008, 09:21 AM -
printing two smallest numbers from a series of numbers
By trofyscarz in forum New To JavaReplies: 2Last Post: 10-14-2008, 11:46 PM -
Counting Pixels
By shaungoater in forum Java 2DReplies: 5Last Post: 11-29-2007, 05:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks