java - for loop to convert integer values into letters -
i working on homework assignment encodes , decodes letters int's , vice versa. first encoding loop have works great!
public static string encode(string msg){ string result=""; string m = msg.touppercase(); char c; int x; (int = 0; i<m.length(); i++){ c = m.charat(i); x = c; if (x == 32){ x = 0; } else{ x -= 64; if (x < 1 || x > 26){ x = 99; } } result += string.valueof(x) + " "; } return result; }
it takes string msg , runs loop convert integer equivalent. issue decoding. essentially, numbers input entered in comma in between each number. take message array , split remove commas. take , read each value in loop , display letter equivalent. stuck. understand logic. it's reverse of first loop, cannot seem code it. here have:
public static string decode(string msg){ string result = ""; string m = msg; char c; int x; string[] nums = msg.split(","); (int = 0; i<nums.length; i++){ string num = nums[i]; x = integer.parseint(num); if(x <= 0 || x > 26){ x=-1; } result += string.valueof((char)(x + 64)) + " "; } return result; }
i having issues reading length of nums array. haven't had luck rest of it. understand have parse integers well. insight completing appreciated!
since it's homework, i'll give next step; can figure out there :)
in loop:
string num = nums[i]; x = integer.parseint(num);
Comments
Post a Comment