# Calculate the value (ASCII) of each word (sum of the value of the letters) of a sentence within an array

3

I'm doing an exercise that is about calculating the value of each word in a sentence that the user enters. I wanted to know if you could help me know how to do it, thanks! What I want to do is calculate the value of a word, or the same, calculate the sum of each letter in ascii.

I've done this but I do not know if I'm starting the snake by the rattle or it's all wrong, but I know I'm missing what I'm asking to calculate the value of a word, or the same, calculate the sum of each letter in ascii.

``````public class ejercicio6 {

char[] pal;
Paraula p = new Paraula(); //Clase que me define una palabra
Scanner scn = new Scanner(System.in);
private static char[] frase = null;
private static int indice;

LT lt = new LT(); //Clase para poder leer char, lineas, reales y enteros

public void inicio(){
int n = 0;
String frase = new String(); //String donde metemos la frase
}

public static void main(String[] args) { //Main donde inicializamos el juego
ejercicio6 at = new ejercicio6();
at.inicio();
}

public static char leerCarTeclado() { //esta clase sirve para leer del teclado
char res = '.';
if (frase != null) {
res = frase[indice++];
}
return 0;
}

public int ContarPesoPalabra() {
String aux;
aux = new LT().llegirLinia(); //Lee el string
pal = aux.toCharArray(); //Convierte la cadena en un array de chars
int S = 0; //Contador de la suma de lo que vale cada letra en ASCII
int n; //Numero con el que tiene que ser más grande la suma de la palabra
n = scn.nextInt(); //Leemos el numero que nos entra el usuario
if (S != '.') { //Si al recorrer el Array no encontramos un . seguimos
for (int i = 0; i < pal.length; i++) { //lee todo el array
S = S + pal[i]; //Suma el contador
if (S >= n && S != espai) { //Si la suma de la palabra en ASCII o hay espacio,
//no sigas, si no es asi, imprime la palabra
System.out.println(S);
}

}
}
return 0;
}
}
``````

I'm new to this, and I do not know if I'm doing it right.

asked by Pedro 13.12.2018 в 14:44
source

3

If you want to add the ASCII value of the letters of a string:

``````    String cadena = "¡Bienvenido a la comunidad!"; //simple variable para probar
int contador = 0; //esta variable contará la suma del valor ASCII de cada letra
for (int i=0; i < cadena.length(); i++){
}
``````

The `.codePointAt()` method returns the ASCII code of a character in   concrete. The character will be the one that matches the index that is passed   as a parameter to the method.

You can also use the following form:

``````(int)cadena.charAt(i);
``````

Since `charAt` returns the character in the position that matches   the index, indicating that you have to treat the value as `(int)` / integer /   the value is converted to ASCII.

In the case of an arrays of words in which there are no spaces between the words (if there are spaces, the ASCII value of the space will be counted):

``````String[] array = {"¡", "Bienvenido", "a", "la", "comunidad", "!"};
for(int j=0; j < array.length; j++) {
for (int i=0; i < array[j].length(); i++){
}
System.out.println(array[j]+" en ASCII el valor es: "+contador);
}
``````

In case there may be spaces and you do not want to count them, you have to put an if inside the loop:

``````for (int i=0; i < array[j].length(); i++){
if(array[j].charAt(i)!=" "){ //si no es un espacio sumara su valor ASCII