posted 21 years ago
A little number theory here:
+345 = 3*100 + 4*10 + 5*1 = 3*10e2 + 4*10e1 + 5*10e0
So what we have is 3 hundreds, 4 tens and 5 ones. When a user types in the string (Ernest gave good advice for this), your job is to figure out how many ones, tens, hundreds, thousands, ten-thousands... the string of characters represent.
If you start looking at the string of characters from the left, you have to decide if the first charater is in the millions position, or if it's the number of tens, or maybe it's a single digit number and it's the ones count. This is very tough to do.
If you start looking from the right end of the string (i.e. the ones position) the problem is much easier. Set up a counter to keep track of the number of valid digits you have found and initialize it to zero (we're starting with the ones position - or 10 raised to the 0 power).
Now loop through the input string starting with charAt( numString.length() - 1) - that's the last character in the string, and work your way backwords to the begining of the string. For each character, you will need to check if it is a valid numeric digit (i.e. between 0 and 9). If it is not a valid character, print your illegal input message. If it is valid, we need to convert it to a number so we can multiply it by 10 raised to the power of what digit position we are in (our valid digit count).
If you look at the ASCII character table, you will see that '0' has the value of decimal 48, '1' is 49, '2' is 50, all the way through '9' having a value of 57. In Java, chars are represented by their ASCII values (actually Unicode values, but in this case they are the same) so '8' is equal to the number 56. We want it to be the number 8 so we can mathematically multiply it by the correct power of ten.
How do we convert the ASCII 56 to numeric 8 without using parseInt? It's easy, we just subtract "0" from it! In other words, 56 - 48 = 8!
So, if we were working on the string above (+345), we'd get the length of the string (i.e. 4) looping from the string from the right, charAt(3) = '5' = 53. Check if the character is a valid numeric digit by testing if it is >= '0' and <= '9', if it is valid, then add it "position amount" to the number sum. It's position amount is equal to it's numerical value (i.e 5) times 10 raised to it's position (since this it the last digit in the string, it's in the ones position or the 10 raised to the valid digit count - which is currently 0).
Since we found and processed a valid digit, increment our position counter (valid digit count sould now be 1 since we are working on the 10 positionn now), and start our string loop again to get the next to the last character. This character is '4'. Check if it is valid. If it is, add it's position amount to the number sum, and increment the valid digit count.
Repeat this until we are looking at the first character in the string. Here, in addition to checking if it is a valid number character, whe also need to check if it is a plus or minus sign. If it is a minus sign, we just multiply our number sum by -1. We can ignore the plus sign or choose to multiply our number sum by +1 (which will do nothing, but does make our code complete). If the character is not a + or -, we process it just the same way we processed all the other characters. Our loop should now stop automatically since we've reached the begining of the string. Note that extra zeros at the beginning of the string are handled automatically - we just add 0*10 raised to whatever position we are in to our number sum).
Finally, if we've made it this far (i.e. we have stopped processing because we found an invalid character), all you need to do is print the number sum!
Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>