Write an application that uses an Array to store 10messages of type String. You
ID: 3687501 • Letter: W
Question
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!” 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() 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;
/**
* @author Srinivas Palli
*
*/
public class StringCannedMessage {
public static void main(String[] args) {
String stringArr[] = { "I love Java the programming language!",
"I love Java the drink!",
"The JRE consists of the Java Virtual Machine (JVM)",
"Java was created by a team led by James Gosling",
"Java is Easy to Use", "Java is Reliability", "Java is Secure",
"Java is Platform Independent", "Java is Object Oriented",
"Java is High level language" };
String selectedMessage = shoutOutCannedMessage(stringArr);
System.out.println("You Selected :" + selectedMessage);
}
/**
* method to return selected message
*
* @param stringArr
* @return
*/
public static String shoutOutCannedMessage(String stringArr[]) {
Scanner scanner = null;
try {
scanner = new Scanner(System.in);
for (int i = 0; i < stringArr.length; i++) {
System.out.println((i + 1) + ". " + stringArr[i]);
}
System.out.print("Select Message :");
int n = scanner.nextInt();
if (n > 0 && n <= 10)
return stringArr[n - 1];
else
return "Invalie Message Selection";
} catch (Exception e) {
// TODO: handle exception
}
return null;
}
}
OUTPUT:
1. I love Java the programming language!
2. I love Java the drink!
3. The JRE consists of the Java Virtual Machine (JVM)
4. Java was created by a team led by James Gosling
5. Java is Easy to Use
6. Java is Reliability
7. Java is Secure
8. Java is Platform Independent
9. Java is Object Oriented
10. Java is High level language
Select Message :9
You Selected :Java is Object Oriented
1. I love Java the programming language!
2. I love Java the drink!
3. The JRE consists of the Java Virtual Machine (JVM)
4. Java was created by a team led by James Gosling
5. Java is Easy to Use
6. Java is Reliability
7. Java is Secure
8. Java is Platform Independent
9. Java is Object Oriented
10. Java is High level language
Select Message :11
You Selected :Invalie Message Selection
1. I love Java the programming language!
2. I love Java the drink!
3. The JRE consists of the Java Virtual Machine (JVM)
4. Java was created by a team led by James Gosling
5. Java is Easy to Use
6. Java is Reliability
7. Java is Secure
8. Java is Platform Independent
9. Java is Object Oriented
10. Java is High level language
Select Message :0
You Selected :Invalie Message Selection
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.