How to number the alphabet and add your letters to get a result for each written word?

5

I wanted to get the result of words with their added letters ...

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

Because you have added the 3 letters a+b+c=6 .

I tried this code but it does not 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);    

  }  
}
    
asked by anonymous 13.09.2016 / 01:55

2 answers

8

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

1- Create a method that counts the 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 well. The big balcony here is to check if the character has a numerical mapped value and add it to the totalizer. This can be done in many ways, for simplicity you can use 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;
}

Using% w / o%, the numerical value check logic of the character is only the call of the Map method.

2- Print the sum of the word:

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

Now you're actually printing the sum, 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;
}

One tip is to always get a problem and break it down into resolution steps, so you can solve a bigger problem by solving each step of it, which makes it easier to work out the overall solution.

    
13.09.2016 / 02:43
7

It's quite 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);    
    }  
}

See running on ideone .

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 can be simple. There are problems that are inherently complex and we can only manage them well. Complicated solutions can and should be avoided.

    
13.09.2016 / 02:51