This line just outputs the message unchanged to the new file.
out2.printf("%s",message);
Also the loop is strange too: why do you read it word by word, and then use String.replace()? You could do it line by line, using String.replaceAll():
while(h.hasNextLine()){
String message=b.nextLine();
out2.printf("%s",message.replaceAll("(^|\\W)are(\\W|$)"," ARE "));
}
The (^|\\W)are(\\W|$)
string is a regular expression, having the meaning to match all content, that starts with either being the start of the string ^
, or a non-word character (\\W
), the string are
, and ends with a non-word character or the end of line($
)...
As scanner has whitespace as the default delimiter, it might be ever better to use (^|\\s)are(\\s|$)
, however both these will replace the whitespace before and after "ARE" with a single space ()...
Also, keep in mind, that String.replace does not mutate the input String... You have to assign the result, or use it any other way, like pass it to a function...