i'm making program receives string input , returns "sum" of values each letter of each word.
for example, input of "take advantage, best, don't stress."
, return:
do(19) take(37) dont(43) best(46) advantage(75) your(79) stress(100)
"do"
have value of 19 because letter "d"
has value of 4 (it fourth letter of alphabet), , "o" has value of 15, total 19.
now store these values have 2 arrays, 1 string array each word, , 1 int array point value have. however, have far:
take(37) advantage(75) do(19) your(79) best(46) dont(53) stress(100)
as can see, not sorted in ascending order trying do. display these values this:
system.out.print(words[j] + "(" + points[j] + ")" + " ");
where words string array , points int array. how can sort them?
my current code:
public static void main (string[] args) { string input = "take advantage, best, don't stress."; string output = ""; //get rid of punctuation for(int = 0; < input.length(); i++){ if( ( (int)input.charat(i) >= 65 && (int)input.charat(i) <= 90) || (int)input.charat(i) == 32 || ( (int)input.charat(i) >= 97 && (int)input.charat(i) <= 122)){ //handles uppercase if(input.charat(i) >= 65 && input.charat(i) <= 90){ int temp = (int)input.charat(i) + 32; char c = (char)temp; output += c; } //handles other characters else{ output += input.charat(i); } } } //done punctuation string[] words = output.split(" "); int[] points = new int[words.length]; //points assignment for(int j = 0; j < words.length; j++){ for(int k = 0; k < words[j].length(); k++){ points[j] += (int)words[j].charat(k) - 96; } system.out.print(words[j] + "(" + points[j] + ")" + " "); } }
how storing results in map<string,integer>
instead of 2 lists:
map mymap = new hashmap<string,integer>;
from there can sort map values: sort map<key, value> values (java)
next can iterate through sorted map:
for(string s : mymap.keyset()){ system.out.println(s+"("+mymap.get(s)+")"); }
Comments
Post a Comment