the project Write a Java application program that will get a sentence from the u
ID: 3888321 • Letter: T
Question
the project Write a Java application program that will get a sentence from the user containing the word "hate" and will change the word into "love". The following is an example of what your MIGHT see on the screen when your program runs. The exact output depends on what values that the user types in while the program runs. The user's inputted values are shown below in italics: Enter a sentence containing the word "hate": i hate green beans Your revised sentence is: i love green beans Here is another example program run: Enter a sentence containing the word "hate": do you think she hates me Your revised sentence is: do you think she loves me Here is another example program un Enter a sentence containing the word "hate": hate is such a strong word Your revised sentence is: love is such a strong wordExplanation / Answer
Please revert back if you need any further help
Code
import java.util.Scanner;
public class ReplaceHate {
public static void main(String[] args) {
//Scanner to take input from user
Scanner in = new Scanner(System.in);
//Asking user to enter sentence
System.out.println("Enter a sentence containing the word "hate":");
//Taking input from user
String sentence = in.nextLine();
//System.out.println(sentence);
//finding the index of first occurrence of word hate
int index = sentence.indexOf("hate");
//storing first and second part in different strings
String first_part = sentence.substring(0, index);
String second_part = sentence.substring(index + 4);
//forming a new string which has love instead of hate
String replaced_Sentence = first_part + "love" + second_part;
System.out.println(replaced_Sentence);
}
}
Sample output
Enter a sentence containing the word "hate":
do you think she hates me?
do you think she loves me?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.