The situation : I have a string and if a word is considered to be a mistake it has to be replaced by "SPELLING_MISTAKE".How do i resolve if a word is a mistake or not?I have a string array with all the correct words.So when i trace the wrong word the progam behaves good and reaches the line where the replacement is meant to be done.But no replacement is done :/ Why?What i am missing?
package essaycorrection; public class Phrase { /** * **************************Variables-fields**************************** */ private int noOfWords; private String content; private static final String[] correctWords = {"All", "Work", "And", "No", "Play", "Makes", "Jack", "A", "Dull", "Boy"}; private static final String[] wrongWords = {"Samaras", "Theodor", "George", "Kostoula", "Charalampos", "Pokemon", "Lapras", "Kahn", "Charizard", "Skoupi"}; /** * **************************Constructor**************************** */ public Phrase(int N) { content = ""; noOfWords = 1 + (int) (Math.random() * N);//numbers from [1,N] int select;//pick correct or wrong word for (int i = 0; i < noOfWords; i++) { select = (int) (Math.random() * 2); if (select == 1) { content += correctWords[(int) (Math.random() * noOfWords)] + " "; } else { content += wrongWords[(int) (Math.random() * noOfWords)] + " "; } } System.out.println("I just composed a phrase with content : " + content + "."); } /** * **************************Accessors**************************** */ public int getNoOfWords() { return noOfWords; } public String getContent() { return content; } public String[] getCorrectWords() { return correctWords; } public String[] getWrongWords() { return wrongWords; } /** * **************************Methods**************************** */ void spellCorrect() { String temp = "";//holds a word at the time in order to check the correctness char letter; boolean mistake; int i = 0; while (i < content.length()) { if ((letter = content.charAt(i)) != ' ') { temp += letter; } else { mistake = true; for (int j = 0; j < correctWords.length; j++) { if ((temp.compareToIgnoreCase(correctWords[j])) == 0) { mistake = false; } } if (mistake) {System.out.println("edw1 "+temp ); content.replaceAll(temp, "SPELLING_MISTAKE"); } temp=""; } i++; } } public void print() { System.out.println(content+"."); } }
in main
Phrase p = new Phrase(5); p.spellCorrect(); p.print();
and the output
I just composed a phrase with content : Samaras Samaras Samaras . edw1 Samaras edw1 Samaras edw1 Samaras Samaras Samaras Samaras . BUILD SUCCESSFUL (total time: 0 seconds)
Also tried replaceFirst but no change in output!