Hey everyone, I am very new to Java and I am learning how to change a specific word from a line of text that is inputted by the user. For example, if I created the program to change the word "hate" to the word "love" (which is what the program is doing) how can I make the program i created more efficient?
This is the main portion of my program...
NOTE: I feel like there should be a way to do this program without having to create a seperate String called subsection. Thanks for the help, it is greatly appreciated!Code:String sentence, subsentence;
int position;
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter a line of text.");
System.out.println("I will change the word hate to the word
love:");
sentence = keyboard.nextLine();
subsentence = sentence;
System.out.println("I am now checking to see if \"hate\" is in
your sentence.");
System.out.println("...");
System.out.println("..");
System.out.println(".");
System.out.println(sentence + " is now changed to");
position = subsentence.indexOf("hate ");
subsentence = subsentence.substring(0, position) + "love";
position = position + 4;
sentence = sentence.substring(position);
System.out.println(subsentence + sentence);

