|
Re: I am just in Java.. Please help
|
Posted: Apr 4, 2002 10:33 PM
|
|
The problem has a few things in it... 1 separate alpa and numeric characters, 2 parse numeric characters into numbers 3 add numbers. 4 concatenate the alpha characters.
toolbox: 1 java.lang.String.charAt() and or java.lang.String.substring() boolean isnumber = java.lang.Character.isDigit(char c); or if("0123456789".indexOf(String singleStringCharacter)>0) itsadigit ; String.length()
2 int k= Integer.parseInt(String digsequence);
3 sum+=k; 4 java.lang.String.concat(anotherString); or simply StringA+=stringb;
Aditionally, you have to figure out whether to insert " " spaces bewteen characters, and if you want to add single digit numbers, or let the size of the numbers be determined by the character separation.
The basic approach: Two Strings, one to make into a sentence, and one to parse into a number for the sum. one number for the sum.
Either a for loop, or a while loop to run through the characters of the string. read the loop one character at a time, if it's a character add it to the sentence string. if(the char was a digit )parse the digit string and add it to the sum. set the parse string to ""; else if its a digit append it to the parseing string. if the last char was alpha add a " " to the alpha.
This explaination is longer than the actual code would be, coding is often more efficient that way, but I think this should help.
|
|