How to number the alphabet and add its letters getting a result for each written word?

Asked

Viewed 6,860 times

7

Wanted to get the result of words with their letters summed...

Example: a=1 b=2 c=3 if I type the word "bac" the result would be "6".

Because you added up the three letters a+b+c=6.

I tried that code but it doesn’t work:

public static void main(String[] args){

Scanner on = new Scanner(System.in); 
System.out.println("Digite a palavra: ");

String soma;

soma = on.nextLine();

char a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;

a=1; b=2; c=3; d=4; e=5; f=6; g=7; h=8; i=9; j=10; k=11; l=12; m=13; 
n=14; o=15; p=16; q=17; r=18; s=19; t=20; u=21; v=22; w=23; x=24; y=25; z=26;   

System.out.println(soma);    

  }  
}

2 answers

10


What your code is doing is simply printing what someone types. You need to add the sum logic and mapping of the alphabet. For this, you can make the following modifications:

1- Create a method that accounts for sums:

private static int calcularSomaPalavra(String palavra, Map<Character, Integer> alfabeto) {
    int valorSoma = 0;
    for (char caractere : palavra.toCharArray()) {
        if (isCaractereEncontrado(alfabeto, caractere))
            valorSoma += getValorCaractere(alfabeto, caractere);
    }
    return valorSoma;
}

What was done was to create a method that given a word gets the sum of the characters of the word, simple as that. The big thing here is to check if the character has a numerical value mapped and add it to the totalizer. This can be done in many ways, for simplicity you can use a Map:

// Declaração do map de letras do alfabeto.
Map<Character, Integer> alfabeto = new HashMap<Character, Integer>();

//Mapeamento dos valores de cada letra do alfabeto
alfabeto.put('a', 1);
alfabeto.put('b', 2);
...
alfabeto.put('z', 26);

private static Integer getValorCaractere(Map<Character, Integer> alfabeto, char caractere) {
    return alfabeto.get(caractere);
}

private static boolean isCaractereEncontrado(Map<Character, Integer> alfabeto, char caractere) {
    return getValorCaractere(alfabeto, caractere) != null;
}

With the use of a Map, the numerical value of the character checking logic is only the method call get.

2- Print the sum of the word:

int valorSoma = calcularSomaPalavra(soma, alfabeto);
System.out.println(valorSoma);

Now you are actually printing the sum, and not just what the user types. Putting it all together, we would have:

public static void main(String[] args) {
    Map<Character, Integer> alfabeto = new HashMap<Character, Integer>();
    Scanner on = new Scanner(System.in);
    System.out.println("Digite a palavra: ");

    String soma;

    soma = on.nextLine();

    alfabeto.put('a', 1);
    alfabeto.put('b', 2);
    ...
    alfabeto.put('z', 26);

    int valorSoma = calcularSomaPalavra(soma, alfabeto);
    System.out.println(valorSoma);

}

private static int calcularSomaPalavra(String palavra, Map<Character, Integer> alfabeto) {
    int valorSoma = 0;
    for (char caractere : palavra.toCharArray()) {
        if (isCaractereEncontrado(alfabeto, caractere))
            valorSoma += getValorCaractere(alfabeto, caractere);
    }
    return valorSoma;
}

private static Integer getValorCaractere(Map<Character, Integer> alfabeto, char caractere) {
    return alfabeto.get(caractere);
}

private static boolean isCaractereEncontrado(Map<Character, Integer> alfabeto, char caractere) {
    return getValorCaractere(alfabeto, caractere) != null;
}

A tip is to always take a problem and divide it into steps of resolution, so you can solve a bigger problem by solving every step of it, which facilitates the elaboration of the global solution.

  • Hello @Giuliana Bezerra Thank you so much!!!! Very good your explanation!!!! Really very much show!!! You are Awesome!!!!

9

It’s pretty simple using math:

import java.util.Scanner;

class Ideone {
    public static void main(String[] args) {
        Scanner on = new Scanner(System.in); 
        System.out.println("Digite a palavra: ");
        String texto = on.nextLine();
        int soma = 0;
        for (char caractere : texto.toCharArray()) { //varre cada caractere
            //"A" vale 97, então tira 96 e assim por diante
            soma += caractere > 96 && caractere < 123 ? caractere - 96 : 0;
        }
        System.out.println(soma);    
    }  
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Other things can be done differently, but the question does not give clear requirements.

The important thing is that this solution uses the principle of KISS. Never complicate what may be simple. There are problems that are inherently complex and we can only manage them well. Complicated solutions can and should be avoided.

  • Thank you so much for the answer!!! @bigown

Browser other questions tagged

You are not signed in. Login or sign up in order to post.