How to resolve the algorithm Abbreviations, easy step by step in the Java programming language
How to resolve the algorithm Abbreviations, easy step by step in the Java programming language
Table of Contents
Problem Statement
This task is an easier (to code) variant of the Rosetta Code task: Abbreviations, simple.
For this task, the following command table will be used:
Notes concerning the above command table:
For a user string of: the computer program should return the string:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Abbreviations, easy step by step in the Java programming language
This Java program takes user input and tries to match it to a list of commands. The list of commands is stored in a COMMAND_TABLE
string and split into an array of words. Each word in the array is then stored in a HashMap
, along with the number of capital letters in the word.
The user is then prompted to enter a command. The program splits the user's input into an array of words and iterates over the words. For each word, the program checks if there is a matching word in the HashMap
. If there is a match, the program prints the matching word. If there is no match, the program prints an error message.
Here is a detailed breakdown of the program:
- The
main
method declares aScanner
object namedinput
. - The
main
method declares a string namedCOMMAND_TABLE
that contains a list of commands. - The
main
method splits theCOMMAND_TABLE
string into an array of words and stores the array in aString[]
namedcmdTableArr
. - The
main
method creates aHashMap
namedcmd_table
to store the words in thecmdTableArr
array and the number of capital letters in each word. - The
main
method iterates over the words in thecmdTableArr
array and stores each word in thecmd_table
HashMap
. The value associated with each word is the number of capital letters in the word. - The
main
method prompts the user to enter a command. - The
main
method splits the user's input into an array of words and stores the array in aString[]
nameduser_input
. - The
main
method iterates over the words in theuser_input
array. - For each word in the
user_input
array, themain
method iterates over the keys in thecmd_table
HashMap
. - If the length of the word is greater than or equal to the number of capital letters in the key and the length of the word is less than or equal to the length of the key, the
main
method checks if the word starts with the key. - If the word starts with the key, the
main
method prints the key and a space. - If the word does not start with the key, the
main
method continues to the next key in theHashMap
. - If the
main
method has iterated over all the keys in theHashMap
and has not found a match, themain
method prints an error message.
Here is an example of the program output:
Please enter your command to verify: si
SI
Source code in the java programming language
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class AbbreviationsEasy {
private static final Scanner input = new Scanner(System.in);
private static final String COMMAND_TABLE
= " Add ALTer BAckup Bottom CAppend Change SCHANGE CInsert CLAst COMPress COpy\n" +
" COUnt COVerlay CURsor DELete CDelete Down DUPlicate Xedit EXPand EXTract Find\n" +
" NFind NFINDUp NFUp CFind FINdup FUp FOrward GET Help HEXType Input POWerinput\n" +
" Join SPlit SPLTJOIN LOAD Locate CLocate LOWercase UPPercase LPrefix MACRO\n" +
" MErge MODify MOve MSG Next Overlay PARSE PREServe PURge PUT PUTD Query QUIT\n" +
" READ RECover REFRESH RENum REPeat Replace CReplace RESet RESTore RGTLEFT\n" +
" RIght LEft SAVE SET SHift SI SORT SOS STAck STATus TOP TRAnsfer Type Up";
public static void main(String[] args) {
String[] cmdTableArr = COMMAND_TABLE.split("\\s+");
Map<String, Integer> cmd_table = new HashMap<String, Integer>();
for (String word : cmdTableArr) { //Populate words and number of caps
cmd_table.put(word, countCaps(word));
}
System.out.print("Please enter your command to verify: ");
String userInput = input.nextLine();
String[] user_input = userInput.split("\\s+");
for (String s : user_input) {
boolean match = false; //resets each outer loop
for (String cmd : cmd_table.keySet()) {
if (s.length() >= cmd_table.get(cmd) && s.length() <= cmd.length()) {
String temp = cmd.toUpperCase();
if (temp.startsWith(s.toUpperCase())) {
System.out.print(temp + " ");
match = true;
}
}
}
if (!match) { //no match, print error msg
System.out.print("*error* ");
}
}
}
private static int countCaps(String word) {
int numCaps = 0;
for (int i = 0; i < word.length(); i++) {
if (Character.isUpperCase(word.charAt(i))) {
numCaps++;
}
}
return numCaps;
}
}
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the ACL2 programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the VBA programming language
You may also check:How to resolve the algorithm Gamma function step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm Towers of Hanoi step by step in the FutureBasic programming language