Jave Question: Write an application that uses an Array to store 10 messages of t
ID: 3776970 • Letter: J
Question
Jave Question:
Write an application that uses an Array to store 10 messages of type String. You can load this data structure 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!” You can initialize your Array with the messages or have the user enter the messages. The choice is yours. Once your Array is loaded with 10 messages, use a loop to iterate through your entire list and display each message. Your Java code will contain a method called shoutOutCannedMessage() that will loop through the Array to iterate through your entire list and display each message and allow the user to select one. The shoutOutCannedMessage() will return the selected message String.
Explanation / Answer
import java.util.Scanner;
import java.io.*;
public class msgStrings
{
public String shoutOutCannedMessage(String msgs[])
{
Scanner charread =new Scanner(System.in);
char s;
for(int i=0;i<msgs.length;i++)
{
System.out.println("Do you wanna select this message?[Y/N]");
System.out.println(msgs[i]);
s=charread.next().charAt(0);
if(s=='y'||s=='Y')
return msgs[i];
}
return null;
}
public static void main(String[] args)
{
//Create a string array to store 10 messages
String msgs[]=new String[10];
String str;
Scanner read = new Scanner(System.in);
msgStrings obj= new msgStrings();
System.out.println("Enter the messages you want...!");
//loop to read the messages
for (int i = 0; i <msgs.length; i++)
{
msgs[i] =read.nextLine();
}
//List of read messages
System.out.println("Messages you entered are....");
for (int i = 0; i < msgs.length; i++)
{
System.out.println(msgs[i]);
}
str=obj.shoutOutCannedMessage(msgs);
System.out.println("Selected message is..."+" "+str);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.