gratiartis
Posts: 8
Nickname: gratiartis
Registered: Sep, 2003
|
|
Re: help~!!!
|
Posted: Sep 10, 2003 7:50 AM
|
|
Given that you're receiving this as a String, you should be able to do the following:public static void processNumber( String numberString ) {
char[] chars = numberString.toCharArray() ;
int digit ;
int zeroCount = 0 ;
int oddCount = 0 ;
int evenCount = 0 ;
for ( int i = 0 ; i < chars.length ; i++ ) {
digit = Character.getNumericValue( chars[ i ] ) ;
if ( digit == 0 ) {
zeroCount++ ;
} else if ( i % 2 == 0 ) {
evenCount++ ;
} else {
oddCount++ ;
}
}
System.out.println( "zeroCount=" + zeroCount
+ ", evenCount=" + evenCount
+ ", oddCount=" + oddCount ) ;
}
I think most of that is self-explanatory, so I haven't bothered commenting it. Basically, I converted the String to an array of char and then just iterated through the array checking whether the numeric value of the char was odd, even or zero. If you're provided with an int to start with, you can easily convert that to a String to pass to this method using Integer.toString( theInt )
|
|