Hashmap to pick a value

Asked

Viewed 302 times

0

I have the following code: it is used to count how many seconds it takes to type a sentence. If I type BABA the result is 10. BUT I WISH THAT WHEN THERE WAS THE JOINING OF THE LETTER 'Q' + 'U,' (AS IN THE WORD CHEESE), HE WOULD CONSIDER A VALUE ONLY AND NOT THE VALUE OF 'Q' + THE VALUE OF 'U'.

I MEAN, 'Q' IN THIS CASE IS WORTH 7 AND U IS WORTH 6, I WANTED THE 'QU' JUNCTION TO BE WORTH ONLY 7, HOW IS IT? TYPE: HASHLETRAS.put("QU", 7);

package solucao;

import java.util.HashMap;

public class Contagem {

public static void main(String[] args) {
    HashMap<String, Integer> hashLetras=new HashMap<String, Integer>();

    hashLetras.put("A", 2); 
    hashLetras.put("B", 3);
    hashLetras.put("C", 4);
    hashLetras.put("D", 5);
    hashLetras.put("E", 3);
    hashLetras.put("F", 4);
    hashLetras.put("G", 5);
    hashLetras.put("H", 6);
    hashLetras.put("I", 4);
    hashLetras.put("J", 5);
    hashLetras.put("K", 6);
    hashLetras.put("L", 7);
    hashLetras.put("M", 8);
    hashLetras.put("N", 9);
    hashLetras.put("O", 5);
    hashLetras.put("P", 6);
    hashLetras.put("Q", 7);
    hashLetras.put("R", 8);
    hashLetras.put("S", 9);
    hashLetras.put("T", 10);
    hashLetras.put("U", 6);
    hashLetras.put("V", 7);
    hashLetras.put("W", 8);
    hashLetras.put("X", 9);
    hashLetras.put("Y", 10);
    hashLetras.put("Z", 11);
    hashLetras.put(" ", 7); //ESPACO

    String teste="QUEIJO";

    int count=0;

    for(int i=0; i<teste.length();i++){

        String c = teste.charAt(i)+ "";
        count = count+hashLetras.get(c);
    }

    System.out.println("O tempo foi de: "+count);
}
}

2 answers

2

Just to be a reference, we can also do how you came up with the idea of hashing the QU because his HashMap is of String. It’s even more organized if you have several combinations of two letters to consider.

For this it stores direct the QU with the intended score:

HashMap<String, Integer> hashLetras=new HashMap<String, Integer>(); 
hashLetras.put("A", 2);
//... tudo o resto
hashLetras.put("QU", 7);

Then for the count you first need to know if you still have a letter forward. If you have, build a String with the two current letters and see if it exists in the hash, and only if it does not exist will use the punctuation of only one letter.

Example:

int count=0;
for(int i=0; i<teste.length();i++){
    int pontosDuasLetras = 0;
    if (i < teste.length() - 1){ //se tem pelo menos mais uma letra para a frente
        String duasLetras = "" + teste.charAt(i) + teste.charAt(i + 1);
        if (hashLetras.containsKey(duasLetras)){
            pontosDuasLetras = hashLetras.get(duasLetras);
            i++; //avança logo as duas letras
        }
    }
    count += pontosDuasLetras == 0 ? hashLetras.get("" + teste.charAt(i)) : pontosDuasLetras;
}

Watch it work on Ideone

In this solution you can add more scores to 2 letters with extreme ease, just adding more keys and values in HashMap.

  • absolutely! Thank you

1


Yes, just do a logical test with one exception, which in this case is if the current char is 'q' and the next one is 'u' and the position of 'q' is up to the penultimate position of the string.

Replace your for for this for:

for(int i=0; i<teste.length();i++){

    if( i<(teste.length()-1) && teste.charAt(i)=='Q' && teste.charAt(i+1)=='U'){
         count = count+7;
         i++;
    }else{
        String c = teste.charAt(i)+ "";
        count = count+hashLetras.get(c);
    }
}
  • That’s right, it worked... only a delay is left when I put: String test="Q U" (ie 'q' space 'u'), it appears a value then updates to another; putting Count = Count+40 with the number 40 da to see right

  • If the answer helped you and is the correct answer could confirm it?

  • very correct answer, very forced

Browser other questions tagged

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