How can I accomplish the same thing/something similar using HashMap in Java? Her
ID: 3576940 • Letter: H
Question
How can I accomplish the same thing/something similar using HashMap in Java?
Here is my current assignment:
"You will create the ShoutBox class for your Virtual World. Your ShoutBox class will have a shoutOutCannedMessage() method that returns a String type. The shoutOutCannedMessage will use an Array or an ArrayList to store 10 messages of type String. For those of you that are more advanced with your Java skills, you could use a HashMap for the data structure. You can load this data structure with 10 messages of your choosing. For example, one message could be “I need Java!” You can initialize your Array or ArrayList with the messages or have the user enter the messages. The choice is yours. The shoutOutCannedMessage() will loop through the data structure to first display all canned messages and allow the user to select one. The shoutOutCannedMessage() will return the selected message String."
While technically what I have accomplishes the assignment, in order to get the highest grade possible for my final project, I want to plan ahead and use HashMap. How might I turn the code I have below into one that functions with HashMap? Any help would be appreciated!
Here is what I have so far that I need to instead utilize HashMap for.
import java.util.Scanner;
public class MessageArray
{
private static final String sentences[]= {"Sent1",
"Sen2",
"Sen3",
"Sen4",
"Sen5",
"Sen6",
"Sen7",
"Sen8",
"Sen9",
"Sen10"
};
public static void main(String[] args)
{
String selected = shoutOutCannedMessage();
System.out.println("You have chosen: "+selected);
}
private static String shoutOutCannedMessage()
{
Scanner input = new Scanner(System.in);
int choice;
for (int start = 0; start < sentences.length; start++)
{
System.out.println((start+1)+")"+sentences[start]);
}
System.out.println("Please enter the number of a sentence to select it: ");
choice = input.nextInt();
return sentences[choice-1];
}
}
Explanation / Answer
Hello, The code is converted with hashmap implementation. The input and output is also verified. Comments are added in the code for easy understanding.
**********************************************************************************************
import java.util.*;
import java.lang.*;
import java.io.*;
class MessageArray
{
static HashMap<Integer,String> hm=new HashMap<Integer,String>(); // declaration of HashMap
public static void intializeHashMap()
{
hm.put(1,"Sent1");
hm.put(2,"Sent2");
hm.put(3,"Sent3");
hm.put(4,"Sent4");
hm.put(5,"Sent5");
hm.put(6,"Sent6");
hm.put(7,"Sent7");
hm.put(8,"Sent8");
hm.put(9,"Sent9");
hm.put(10,"Sent10");
}
public static void main(String[] args)
{
String selected = shoutOutCannedMessage();
System.out.println("You have chosen: "+selected);
}
private static String shoutOutCannedMessage()
{
Scanner input = new Scanner(System.in);
int choice;
Integer x;
intializeHashMap(); // intialization of HashMap
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+")"+m.getValue());
}
System.out.println("Please enter the number of a sentence to select it: ");
choice = input.nextInt();
x=choice;
return hm.get(x);
}
}
**********************************************************************************************
INPUT:
7
OUTPUT:
1)Sent1
2)Sent2
3)Sent3
4)Sent4
5)Sent5
6)Sent6
7)Sent7
8)Sent8
9)Sent9
10)Sent10
Please enter the number of a sentence to select it: 7
You have chosen: Sent7
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.