Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java write a program BuildAndSearch that allows the user to build String cont

ID: 3605192 • Letter: I

Question

 In Java  write a program BuildAndSearch that allows the user to build String contents and search the contents for a key.  The program maintains a StringBuilder object myBuilder during its execution. The program initializes the object with the empty builder          StringBuilder myBuilder = new StringBuilder();  The program interacts with the user to execute one of the following actions on the StringBuilder object:  1. Add more text 2. View text 3. Search for a key  (1) To add more text, the program instantiates a new Scanner with System.in and using a while-loop, receives text lines to append to myBuilder.  It reads each line with the method nextLine and appends the line and a " " after that. In other words, each line of text will be come a line ending with a " ". To indicate the end of the input, the user enters ".", so the continuation condition for the while-loop the method hasNext() on the Scanner returns true. This will be accomplished by a method call          add( myBuilder );  where add is a method with the header          public static void add( StringBuilder builder )  (2) To view the text contents stored in myBuilder, the program prints the String returned by the method toString() on myBuilder (that is, myBuilder.toString()). 

Explanation / Answer

===

import java.util.Scanner;

/**
*
*/

/**
* @author XXXX
*
*/
public class BuildAndSearch {

   @SuppressWarnings("resource")
   public static void add(StringBuilder builder) {
       Scanner read = new Scanner(System.in);
       while (read.hasNextLine()) {
           String line = read.nextLine();
           builder.append(line + " ");
           if (line.contains(".")) {
               break;
           }

       }
   }

   /**
   * @param args
   */
   @SuppressWarnings("resource")
   public static void main(String[] args) {
       StringBuilder myBuilder = new StringBuilder();
       Scanner reader = new Scanner(System.in);
       while (true) {
           System.out.println("1. Add more text");
           System.out.println("2. View text");
           System.out.println("3. Search for a key");
           int choice = Integer.parseInt(reader.nextLine().trim());
           switch (choice) {
           case 1:
               add(myBuilder);
               break;
           case 2:
               System.out.println(myBuilder.toString());
               break;
           case 3:
               String searchWord = reader.nextLine().trim();
               System.out.println("Word: " + searchWord + " EXIST: " + myBuilder.toString().contains(searchWord));
               break;
           default:
               break;
           }

       }
   }

}

=====

1. Add more text
2. View text
3. Search for a key
1
srinu
sri
s
.
1. Add more text
2. View text
3. Search for a key
2
srinu
sri
s
.

1. Add more text
2. View text
3. Search for a key
3
sri
Word: sri EXIST: true
1. Add more text
2. View text
3. Search for a key


===

Thanks

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote