Question

Write a method deleteMsg that receives your messages, their total number and a message that you...

Write a method deleteMsg that receives your messages, their total number and a message that you would like to remove. The method then searches for the message and delete it from your messages history. If the message was not found, an error message is reported. java

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//Although a little more elaboration is needed to perfectly 
//understand the requirements, I tried my best to comprehend and implement.

package HomeworkLib1;
import java.util.ArrayList;
import java.util.Scanner;

public class Message {
        ArrayList<String> messages;
        Message(){
                messages = new ArrayList<String>();
                System.out.println("Message box created and set up.");
        }
        public static void main(String[] args) {
                Message m = new Message();                              //New Message box created
                m.messages.add("Hello there");          //Adding some messages to the message box
                m.messages.add("I am sme");
                m.messages.add("I work for HomeworkLib");
                m.messages.add("This is a java question");
                
                Scanner scan = new Scanner(System.in);
                String option="";
                boolean flag = false;
                
                do{
                        System.out.println("Do you want to delete a message? yes/no");
                        option = scan.nextLine();
                        if(option.contentEquals("yes")||option.contentEquals("no"))
                                flag = true;
                }while(!flag);
                if(option.contentEquals("yes")) {
                        System.out.println("Enter a message to search and delete: ");
                        
                        deleteMessage(m.messages);
                }

        }

        public static void deleteMessage(ArrayList<String> messages) {
                Scanner scan = new Scanner(System.in);
                String msg = scan.nextLine();
                boolean flag = false;
                for(int i=0; i<messages.size() ; i++) {
                        if(messages.get(i).contentEquals(msg)) {
                                flag = true;                    //Message found
                                messages.remove(i);             //Delete message
                        }
                }
                if(flag)
                        System.out.println("Message deleted.");
                else
                        System.out.println("Message not found.");
        }
}

Add a comment
Know the answer?
Add Answer to:
Write a method deleteMsg that receives your messages, their total number and a message that you...
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
  • java Write an application that will show 5 different messages (no input, 5 types: error message,...

    java Write an application that will show 5 different messages (no input, 5 types: error message, question message, information message, warning message and plain message). Use the welcome.java example from lecture slides. You should end up with just one file. Icon Code IDE Value JOptionPane.PLAIN MESSAGE JOptionPane.ERROR MESSAGE No icon -1 X 0 JOptionPane.INFORMATION MESSAGE 1 JOptionPane.WARNING MESSAGE 2 JOptionPane QUESTION MESSAGE 3 Write an application that will show 5 different messages (no input, 5 types: error message, question message,...

  • Problem 4. Messages arrive at a node in a communication system. A message is corrupted by...

    Problem 4. Messages arrive at a node in a communication system. A message is corrupted by noise with probability p-005. Let X be the number messages until the first message in error is read. How would you justify the clain that X is a geometric random variable with parameter p 0.05

  • Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has...

    Write a program that prompts a user, using Scanner, for 2 secret messages. Each message has its own set of rules you need to use to decode it. You will use String methods to change the messages you receive from the user. *Note: Ensure that you make the changes in the order they are specified and do the operations on the updated String Sentence 1: Make everything after the first word lowercase Remove the last space Remove the first 3...

  • a. Write a method that receives (a reference to an array of integers and returns the...

    a. Write a method that receives (a reference to an array of integers and returns the number of odd values in the array that are between 20 and 35 (inclusive) or greater than 65 (also b. Write a single Java expression in Java that corresponds to the following formula: 2 (y3 – 5) - VZ)10)

  • Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sale...

    Looking for some help with this Java program I am totally lost on how to go about this. You have been approached by a local grocery store to write a program that will track the progress of their sales people (SalesPerson.java) The information needed are: D Number integer Month 1 Sales Amount-Double Month 2 Sales Amount-Double Month 3 Sales Amount-Double Write a Java program that allows you to store the information of any salesperson up to 20 While the user...

  • Storing the Array: Write an application that uses an Array to store 10messages of type String....

    Storing the Array: Write an application that uses an Array to store 10messages of type String. You will store this Array with 10 messages of your choosing. For example, a message could be “I love Java the programming language!” or another message could be “I love Java the drink!” Initializing the Array: You may initialize your Array with the messages or have the user enter the messages. The choice is yours. Your Java code will contain a method called shoutOutCannedMessage()...

  • Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, o...

    Please use C++,thank you! Write an AddressBook class that manages a collection of Person objects. Use the Person class developed in question 2. An AddressBook will allow a person to add, delete, or search for a Perso n object in the address book The add method should add a person object to the address book. The delete method should remove the specified person object from the address book 0 .The search method that searches the address book for a specified...

  • In Problem Set 7 you designed and implemented a Message class. This time, let's design and...

    In Problem Set 7 you designed and implemented a Message class. This time, let's design and implement a Mailbox class in a file named Mailbox java. Do the following with this class • You may use the Message class from PS 7. You will have to add new features to the Message class from PS 7 as you work through this problem. You are welcome to start with my sample solution if you wish • Suppose there are multiple mail...

  • MESSAGE QUEUES WITH RABBITMQ

    In this assignment you will need to install Erlang (www.erlang.org/downloads) and the RabbitMQ server (https://www.rabbitmq.com/download.html) for your operating system. Finally, for protocol support you will need to install the Pika module. From PyCharm inside your project go to File > Settings > Project Settings > Project Interpreter. From there click on the green “+” to add a package to the project. Enter Pika in the search field at the top and select Add Package. Now, you will need two Python source...

  • Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes...

    Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes as argument an integer corresponding to a number of seconds, computes the exact time in hours, minutes and seconds, then prints the following message to the screen: <inputseconds> seconds corresponds to: <hour> hours, <minute> minutes and <second> seconds Write another method called in Seconds that takes as arguments three integers: hours, minutes and seconds, computes the exact time in seconds, then returns the total...

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