android - Java help needed for a code like Autocorrect -
i writing program opposite of auto correct. logic user enters sentence, when button pressed, grammatical opposite of sentence entered user should displayed. able code. used matcher logic.but not able desired output. linking code question. can me please?
imagebutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string input = edittext.gettext().tostring(); string store = input; string store1 [] = store.split(" "); string correct[] = {"is","shall","this","can","will"}; string wrong [] = {"was","should","that","could","would"}; string output = ""; (int i=0;i<store1.length;i++) { for(int j=0;j<correct.length;j++){ if(store1[i].matches(correct[j])) { output = input.replace(store1[i], wrong[j]); //store1[i] = wrong[j]; } else { input.replace(store1[i], store1[i]); //store1[i] = store1[i]; } } mtextview.settext(output); } }});
by looking @ code, i've found redundancy , unused variable. shown below.
string store = input; string store1 [] = store.split(" ");
as shown below, did cleanup , implement logic using map interface. wrong value must key of map can determine word wrong value using map.containkeys(word). if key found concatenate output variable correct word.
string input = edittext.gettext().tostring().split(" "); map<string, string> pairs = new hashmap<>(); pairs.put("is", "was"); pairs.put("shall", "should"); pairs.put("this", "that"); pairs.put("can", "could"); pairs.put("will", "would"); string output = ""; (string word : input) { if (pairs.containskey(word)) { output = output + pairs.get(word) + " "; } else { output = output + word + " "; } } mtextview.settext(output.trim());
Comments
Post a Comment