I have to rewrite my program using String andScanner classes. I don\'t understan
ID: 3614388 • Letter: I
Question
I have to rewrite my program using String andScanner classes. I don't understand scanner. Could you help me?Thank you. public class lovehate{public static void main (String [] args)
{String phrase="I hate you",replaced;
String word="love",replace="hate";
System.out.println("The line of text to be changed is:");
System.out.println(phrase);
System.out.println("I have rephrased that line toread:");
replaced=phrase.replaceFirst(replace,word);
System.out.println(replaced);
}
} I have to rewrite my program using String andScanner classes. I don't understand scanner. Could you help me?Thank you. public class lovehate{
public static void main (String [] args)
{String phrase="I hate you",replaced;
String word="love",replace="hate";
System.out.println("The line of text to be changed is:");
System.out.println(phrase);
System.out.println("I have rephrased that line toread:");
replaced=phrase.replaceFirst(replace,word);
System.out.println(replaced);
}
}
Explanation / Answer
please rate - thanks import java.util.*; public class lovehate { public static void main (String [] args) {String phrase,replaced; String word,replace; Scanner in= new Scanner(System.in); System.out.print("Enter the original phrase: "); phrase=in.nextLine(); System.out.print("Enter the word to replace: "); replace=in.next(); System.out.print("Enter the word you are replacing it with:"); word=in.next(); System.out.print(" The line of text to be changed is:"); System.out.println(phrase); System.out.print("I have rephrased that line to read:"); replaced=phrase.replaceFirst(replace,word); System.out.println(replaced); } }