Question

Java // Strings

Using the StringBulderClass write an program that takes this text below and substitutes your name every time there is an it or It. The program also should remove the commas and replace them with colons. Then print the resulting string It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way-- in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code Screenshots:

//Define the //class StringModifier. public class StringModifier { //Define the main () method. public static void main (Stri{ int x = obj.indexof (,); obj.replace (x, x+1, :); //0therwise, break //out of the loop. else break; //Display the //mod

Sample Output:

sample name was the best of times: sample name was the worst of times: sample name was the age of wisdom: sample name was the

Code to Copy:

//Define the

//class StringModifier.

public class StringModifier {

     

      //Define the main() method.

      public static void main(String[] args)

      {

            //Define the string

            //to be modified.

            String new_string = "It was the best of times, "

                        + "it was the worst of times, it was "

                        + "the age of wisdom, "

                        + "it was the age of foolishness, it was the "

                        + "epoch of belief, it was the epoch of "

                        + "incredulity, it was the season of Light, "

                        + "it was the season of Darkness, it was the "

                        + "spring of hope, it was the winter of despair, "

                        + "we had everything before us, we "

                        + "had nothing before "

                        + "us, we were all going direct to Heaven, "

                        + "we were all going direct the other way-- "

                        + "in short, the period was so far like the "

                        + "present period, that some of its noisiest"

                        + " authorities insisted on its being "

                        + "received, for good or for evil, "

                        + "in the superlative degree of "

                        + "comparison only.";

           

            //Define an object of the StringBuilder class.

            StringBuilder obj =

                        new StringBuilder(new_string.toString());

           

            //Run the loop to modify the string.

            while(true)

            {

                  //If an instance of "it" is found followed

                  //by a space then, replace

                  //it with the required name.

                  if(obj.indexOf("it ") != -1)

                  {

                        int x = obj.indexOf("it ");

                        obj.replace(x, x+2, "sample_name");

                  }

                 

                  //Otherwise, if an instance of "It"

                  //is found followed by a space then,

                  //replace it with the required name.

                  else if(obj.indexOf("It ") != -1)

                  {

                        int x = obj.indexOf("It");

                        obj.replace(x, x+2, "sample_name");

                  }

                 

                  //Otherwise, if an instance of

                  //',' is found then,

                  //replace it with a ':'.

                  else if(obj.indexOf(",") != -1)

                  {

                        int x = obj.indexOf(",");

                        obj.replace(x, x+1, ":");

                  }

                 

                  //Otherwise, break

                  //out of the loop.

                  else

                  {

                        break;

                  }

                 

            }

           

            //Display the

            //modified string.

            System.out.println(obj.toString());

      }

}

Add a comment
Know the answer?
Add Answer to:
Java // Strings Using the StringBulderClass write an program that takes this text below and substitutes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python: Implement a function myIndex() that takes as input a text file and a list of...

    Python: Implement a function myIndex() that takes as input a text file and a list of words. Search the file and print the word along with the line numbers that word appears in the text as follows: >>> myIndex('raven.txt', ['raven', 'mortal', 'dying', 'ghost', 'ghastly', 'evil', 'demon']) raven     44, 53, 55, 64, 78, 97, 104, 111, 118, 120 mortal    30 dying     9 ghost     9 ghastly   82 evil      99, 106 demon     122 reven.txt: Once upon a midnight dreary, while I pondered weak...

  • Using the book, write another paragraph or two: write 170 words: Q: Compare the assumptions of...

    Using the book, write another paragraph or two: write 170 words: Q: Compare the assumptions of physician-centered and collaborative communication. How is the caregiver’s role different in each model? How is the patient’s role different? Answer: Physical-centered communication involves the specialists taking control of the conversation. They decide on the topics of discussion and when to end the process. The patient responds to the issues raised by the caregiver and acts accordingly. On the other hand, Collaborative communication involves a...

  • please read this articel and write one page summary atleast 250 words: MLB managers learn Spanish...

    please read this articel and write one page summary atleast 250 words: MLB managers learn Spanish to unite teams and clubhouses: At spring training of 1962, the newly hired manager of the San Francisco Giants, Alvin Dark, gathered several of his side's Latin American players together behind second base. Once there, he gave an order that left them surprised, stunned and outraged. "He told us that we couldn't speak Spanish to each other in the clubhouse", said Orlando Cepeda, who...

  • Evaluate the arical writ the response in which you state your agreement or disagreement with writer...

    Evaluate the arical writ the response in which you state your agreement or disagreement with writer up un these questions guidelines 1) can empathy lead us astrary? how 2) our heart will always go out to the baby in the well, its a measure of our humanity. but empathy will have to yield to reason if humanity is to have a future can empathy yield to reason? how? thank you The Baby in the Well: The Case against Empathy* -Paul...

  • FISCAL POLICY IN THEORY: March, 2020: we are on the verge of Congress and the President...

    FISCAL POLICY IN THEORY: March, 2020: we are on the verge of Congress and the President passing legislation that will empower the federal government to spend an unprecedented amount of EXTRA money not seen since World War 2 ---- in order to address the pandemic but also to help cushion the blow financially of perhaps ten or twenty million Americans --- or more --- losing their jobs, and thus suffering a drop in income. The scale of the 2020 recession...

  • Direction: review the eassy prof it and summerize it. Add thesis with it. make changes if...

    Direction: review the eassy prof it and summerize it. Add thesis with it. make changes if posible Title The coporate puzzel When all you believe is people behind you until they turn their back on you. Times are hard when you are not to be worried about anything in life. I had a great job with a very working paid. Things were going smoothly until one day I had a call to my manager’s office saying he will like to...

  • Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America”...

    Read “Instituionalizing our Demise: America vs Multiculturalism” by Roger Kimball on pg 268 and “Reinventing America” Call for a new national indentity” by Elizabeth Martinez on pg 275. Create a double entry notebook for each reading selection It should be atleast five observation and responses. wric 268 PART 2 essay pro. exactly how and why their authors disagree. Instead of with parties in conflict as mediators do, you will nt of view designed to appeal to both sides, mediatn posing...

  • Develop a case study analysis in the following format Relevance of the case study to my...

    Develop a case study analysis in the following format Relevance of the case study to my work environment - application - learning impact CASE 4-1 Tambrands Overcoming Cultural Resistance Tampax, Tambrands's only product, is the best-selling tampon in their virginity if they use a tampon. When they go to the beach in the world, with 44 pencent of the global market North America and tiny bikinis, tampons arent their choike. Instead, hordes of women Europe account for 90 percent of...

  • How can we assess whether a project is a success or a failure? This case presents...

    How can we assess whether a project is a success or a failure? This case presents two phases of a large business transformation project involving the implementation of an ERP system with the aim of creating an integrated company. The case illustrates some of the challenges associated with integration. It also presents the obstacles facing companies that undertake projects involving large information technology projects. Bombardier and Its Environment Joseph-Armand Bombardier was 15 years old when he built his first snowmobile...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT