gratiartis
Posts: 8
Nickname: gratiartis
Registered: Sep, 2003
|
|
Re: help~!!!
|
Posted: Sep 11, 2003 3:00 AM
|
|
> Correction boss : > Use > } else if ( digit % 2 == 0 ) { Hehe ... that'll teach me to test code before I post it ;)
> Moreover this can take any string like "abc"
I did think of that, but I was more interested in showing how to get at each digit in the String. So I didn't bother with any error handling. As mentioned elsewhere in the thread, I also didn't want to provide something that could be handed in as a homework assignment without doing quite a bit of work on it!
I'm not sure of what's the most efficient way to check the validity of the input. As you showed, just trying to instantiate an Integer seems the simplest way of doing it. Otherwise for each digit, you could always do (digit >= 0 && digit <= 9). Or for each char, before getting the number value, you could just do (char >= '0' && char <= '9'). Some kind of exception would be thrown if it's out of the range. Anyone else got any thoughts on this? I was never much of an expert on code efficiency :)
|
|