110k views
0 votes
Write a Java program that asks the user to enter a message. If the message has the words ""attach"" or ""attached"" anywhere in it the program should reply ""don’t forget to attach your file!"" If neither word appears in the message the program should reply ""message sent"".

User Rukletsov
by
7.7k points

1 Answer

7 votes

Final answer:

The task is to create a Java program that prompts the user for a message, checks for the words "attach" or "attached", and responds with a reminder to attach a file if those words are present, or confirms message sent otherwise.

Step-by-step explanation:

The provided question revolves around creating a Java program that interacts with a user by asking for a message. The program will then check the message for certain keywords to determine an appropriate response. Let's construct the basic structure of this program:

import java.util.Scanner;
public class MessageChecker {
public static void String [] args) {
Scanner scanner = new Scanner (System.in);
System.out.println("Enter your message: ");
String message = scanner.nextLine();
if (message.contains("attach") || message.contains("attached")) {
System.out.println("don’t forget to attach your file!");
} else {
System.out.println("message sent");
}
scanner.close();
}
}
In this Java program, we start by importing the Scanner class, which is used to read the user's input. After prompting the user and receiving their message, the program checks if it contains the words "attach" or "attached". If it does, it reminds the user to attach their file. Otherwise, it simply states that the message was sent.

User Ben Butterworth
by
7.8k points