Write an application that uses an Array to store 10 messages

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. Include comment and supporting method in the code.

Solution

StringTest.java


import java.util.Scanner;

public class StringTest {

  
   public static void main(String[] args) {
  
       Scanner scan = new Scanner(System.in);
       String messages[] = new String[10];
       for(int i=0; i<messages.length; i++){
           System.out.print(\"Enter the string: \");
           messages[i] = scan.nextLine();
       }
       String choice = shoutOutCannedMessage(scan, messages);
       System.out.println(\"Your choice is \"+choice);
   }
  
   public static String shoutOutCannedMessage(Scanner scan, String messages[]){
       for(int i=0; i<messages.length; i++){
           System.out.println((i+1)+\": \"+messages[i]);
       }
       System.out.print(\"Enter your choice: \");
       int choice = scan.nextInt();
       return messages[choice-1];
   }

}

Output:

Enter the string: I love Java the drink!
Enter the string: I love Java
Enter the string: Hello Java
Enter the string: Hi Java
Enter the string: Java is aesome
Enter the string: I love Java the programming language!
Enter the string: java is ggod anguage
Enter the string: Java java
Enter the string: Java J2EE
Enter the string: Java OOPs
1: I love Java the drink!
2: I love Java
3: Hello Java
4: Hi Java
5: Java is aesome
6: I love Java the programming language!
7: java is ggod anguage
8: Java java
9: Java J2EE
10: Java OOPs
Enter your choice: 6
Your choice is I love Java the programming language!

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 m
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 m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site