Any help would be appreciated (Java). This program will translate known text mes
ID: 3703488 • Letter: A
Question
Any help would be appreciated (Java).
This program will translate known text message abbreviations to their actual meanings in unabbreviated form.
(1) Implement for Tests 1 - 3:
Read an input string from the user.
If the user's input string matches a known text message abbreviation,
Output the unabbreviated form
Otherwise output: Unknown.
Support two abbreviations:
LOL -- laughing out loud
IDK -- I don't know
Sample input/output:
(2) Implement for Tests 4 - 6:
Expand to also decode these abbreviations.
BFF -- best friends forever
IMHO -- in my humble opinion
Explanation / Answer
Hii, here is the code you wanted. I have implemented the required feature using a HashMap to map each abbreviations to its corresponding translations, and then fetched it (if exists) according to the abbreviation entered by the user. Check the comments for detailed explaination. Thanks.
// TranslateAbbreviations.java
import java.util.HashMap;
import java.util.Scanner;
public class TranslateAbbreviations {
/**
* declaring a Hashmap to map each abbreviations with their translations
*/
static HashMap<String, String> abbrMap;
/**
* scanner to read the user input
*/
static Scanner scanner;
public static void main(String[] args) {
/**
* Initializing the map
*/
abbrMap = new HashMap<String, String>();
/**
* Adding a few abbreviations to the map
*/
abbrMap.put("LOL", "Laughing out loud");
abbrMap.put("IDK", "I don't know");
abbrMap.put("BFF", "Best friends forever");
abbrMap.put("IMHO", "In my humble opinion");
/**
* initializing the scanner
*/
scanner = new Scanner(System.in);
/**
* Prompting the user to enter an abbreviation
*/
System.out.println("Enter an abbreviation: ");
// getting input and converting to upper case
String input = scanner.nextLine().toUpperCase();
/**
* checking if the map contains the entered abbreviation
*/
if (abbrMap.containsKey(input)) {
// displaying the translation
System.out.println(abbrMap.get(input));
} else {
// not found in the list
System.out.println("Abbreviation not found!");
}
}
}
/*OUTPUT1*/
Enter an abbreviation:
LOL
Laughing out loud
/*OUTPUT2*/
Enter an abbreviation:
imho
In my humble opinion
/*OUTPUT3*/
Enter an abbreviation:
WEE
Abbreviation not found!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.