java - How to replace a char with a string -
i know replacing char string can't done, i'm trying make morse code translating program , have 2 arrays 1 letters , 1 morse code translation! have used stringtokenizer , want take every character of word , replace translation of character in morse code! how can done?
here part of code matters:
stringtokenizer tokenizer = new stringtokenizer(line); while (tokenizer.hasmoretokens()) { string token = tokenizer.nexttoken(); if (isword(token)) { (int j = 0; j < token.length(); j++) { char ch = token.charat(j); (int k=0; k<26; k++){ if (ch==((char)letter[k])){ ch=(char)morse[k]; } } } system.out.print(token); } }
you can use stringbuilder this. given have 1 array chars, can iterate on each char , append translated variant stringbuilder object.
example:
char[] chars = {'h', 'e', 'l', 'l', 'o'}; stringbuilder sb = new stringbuilder(); (char c : chars) { sb.append(getmorse(c)); } system.out.println(sb.tostring());
where getmorse() function returns string containing morse code variant of char.
Comments
Post a Comment