It is a common error to to accidentally repeat a word Write

It is a common error to to accidentally repeat a word. Write a program that reads a sentence, a word at a time, and removes any adjacent duplicates. The sentence ends when you read a word that ends in a period. The sentence will have at least two words.

How do I finish this code to answer this question?

import java.util.Scanner;

public class RemoveDuplicateWords
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String word = in.next();
  
boolean done = true;
while (!done)
{
String previous = ;
word = . . .;
if (word.endsWith(\".\"))
{
word = . . .;
done = . . .;
}
if (. . .)
{
System.out.print(previous + \" \");
}
}
System.out.print(word + \".\");
}
}

Solution

There are many errors in the given code, the loop in the code won\'t execute at all, because the done variable is initialised as true and in while condition it is given !done, thus it will alsways false. Instead of this above code, I\'ve just given below a small variation of this given code, which will works as intended. Please find the required program along with its output. Please see the comments against each line to understand the step.

-------------------------------------

OUTPUT:

hi
> hi
hello
> hello
how
> how
how
are
> are
you
> you
you
?
> ?

It is a common error to to accidentally repeat a word. Write a program that reads a sentence, a word at a time, and removes any adjacent duplicates. The sentenc

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site