In Java language write a function named loveHate that plays

In Java language write a function named loveHate that plays the part of the person who knows the trick. loveHate has no args and no return value. It just plays this silly game and returns. So the statement in main to play the program is just
loveHate();// Play the love/hate game with the user
The user repeatedly types a line specifying some thing, and the program says whether it loves it or hates it, according to whether there\'s a doubled character. A blank line is the user\'s way of terminating the game. The user might type a line with several words, so you probably want to do input with the nextLine() method of the Scanner class. An example run of the function might go as:
Name some things, one per line, blank line to quit:
students
I hate students!
exams
   I hate exams!
quizzes
    I love quizzes!
green eggs and ham
    I love green eggs and hams!
<blank line terminates program>
(when your program is given a hateful line(a line with no doubled char), it just says that it hates the thing, it doesn\'t have to think of something similar that it loves. Likewise, when your program is given a lovable line(a line with a doubled char), it just says that it loves the thing, it doesn\'t have to think of something similar that it hates.)
With a sample run program plus!

Solution

LoveHateDemo.java

import java.util.Scanner;

public class LoveHateDemo {

   public static void main(String[] args) {
       //Calling the method
       loveHate();

   }

   /* This method will decides and display the message hates or loves
   * Params : void
   * return : void
   */
   private static void loveHate() {
      
       //Declaring variables
       String str = null;
       int flag = 0;
      
       //Scanner class object is sued top read the inputs entered by the user
       Scanner kb = new Scanner(System.in);
      
       System.out.println(\"Name some things, one per line, blank line to quit: \");
      
       //This loop continue to execute until the user enter a black line
       while (true) {
           //Getting the line entered by the user
           str = kb.nextLine();
      
           //If the user entered a blank line then the program will exit
           if (str.equals(\"\")) {
               break;
           }
           //If the user entered other than blank line then it will decide love or hate
           else {
               for (int i = 0; i < str.length() - 1; i++) {
                  
                   /* If the line has double character the it display love message
                   * If not,display hate message
                   */
                   if (str.charAt(i) == str.charAt(i + 1)) {
                       flag = 1;
              
                   }
                  
               }
               if (flag == 1)
                   System.out.println(\"I Love \" + str + \"!\");
               else if (flag == 0) {
                   System.out.println(\"I Hate \" + str + \"!\");
                  
               }
           }
       }

   }
}

________________________________

Output:

Name some things, one per line, blank line to quit:
students
I Hate students!
exams
I Hate exams!
quizzes
I Love quizzes!
I love green eggs and ham
I Love I love green eggs and ham!

_______Thank You

In Java language write a function named loveHate that plays the part of the person who knows the trick. loveHate has no args and no return value. It just plays
In Java language write a function named loveHate that plays the part of the person who knows the trick. loveHate has no args and no return value. It just plays

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site