首页 > 解决方案 > Is there a better way to store and retrieve the values of letters given by their position in the alphabet?

问题描述

I've got an assignment to write a program that adds the digits of an integer and the letters in a word (the value of each letter is given by its position in the alphabet, e.g. j = 10). After a while of trying to figure out a way to store and retrieve the value of each letter I decided to use a HashMap... but I wonder if there are better ways to do it (I'm sure there are)... so I'd like to get some suggestions on how to improve the code and learn some new things... thanks!

import java.util.HashMap;
import java.util.Scanner;

public class NLS {

    public static HashMap<Character, Integer> alphabet;
    static {
        alphabet = new HashMap<>();
        alphabet.put('a', 1);        alphabet.put('b', 2);         alphabet.put('c', 3);        alphabet.put('d', 4);
        alphabet.put('e', 5);        alphabet.put('f', 6);         alphabet.put('g', 7);        alphabet.put('h', 8);
        alphabet.put('i', 9);        alphabet.put('j', 10);        alphabet.put('k', 11);       alphabet.put('l', 12);
        alphabet.put('m', 13);       alphabet.put('n', 14);        alphabet.put('ñ', 15);       alphabet.put('o', 16);
        alphabet.put('p', 17);       alphabet.put('q', 18);        alphabet.put('r', 19);       alphabet.put('s', 20);
        alphabet.put('t', 21);       alphabet.put('u', 22);        alphabet.put('v', 23);       alphabet.put('w', 24);
        alphabet.put('x', 25);       alphabet.put('y', 26);        alphabet.put('z', 27);
    }

    public static void main(String[] args) {
        char keepGoing;
        do {
            int sum;
            Scanner scan = new Scanner(System.in);
            System.out.println("\n\n\n____________________________________________________________");
            System.out.println("\n\tEnter a number [integer] or a word/sentence [no spaces]:\n");
            String input = scan.nextLine();
            boolean numeric = numberOrWord(input);
            if (numeric) {
                sum = digitSum(Integer.parseInt(input));
                System.out.println("Result= " + sum);
            } else {
                char letter = calculateLetter(input);
                System.out.println("Result= " + letter);
            }
            System.out.println("<<PRESS s/S TO CONTINUE>>");
            keepGoing = scan.next().charAt(0);
        } while (keepGoing == 's'||keepGoing == 'S');
    }

    public static boolean numberOrWord (String str){
        try{
            Double.parseDouble(str);
            return true;
        }catch(NumberFormatException e){
            return false;
        }
    }

    public static int digitSum(int number){
        int sum = 0;
        while(number > 0){
            sum = sum + number % 10;
            number /= 10;
        }
        return sum;
    }

    public static char calculateLetter(String word){
      
        int sumPV = 0;
        for(char digit : word.toCharArray()){
            sumPV += alphabet.get(Character.toLowerCase(digit));
        }

       /* for(int i = 0, l = word.length(); i < l; i++){
            char c = word.toLowerCase().charAt(i);
            sumPV = sumPV + alphabet.get(c);
        }*/

        while(sumPV > 27){
            sumPV =digitSum(sumPV);
        }
        char letter = (char) 0;
        for(char key : alphabet.keySet()){
            if(alphabet.get(key).equals(sumPV)) {
                letter = key;
            }
        }

        return letter;
    }

}



标签: javaalgorithmhashmap

解决方案


Every char is just a mapping to integer value and you can print their intger values to see that A -> 65 by doing simple operations like

char d = 'D'; // D is just 68

so doing

 d - 65 Or d - 'A'

Both result in 3

You can use the integer value representations for char and do integer operations like so

public class Main
{
    public static void main(String[] args) {
        System.out.println("Hello World");
        char a = 'A';
        System.out.println(a - 'A' + 1); //Returns 1
        a = 'B';
        System.out.println(a - 'A' + 1); //Returns 2
    }
}

[Update] For your specific case you just need to replace ñ with O before replacing


推荐阅读