HashMap to get a value

0

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

OR BE, 'Q' IN THIS CASE IS VALLEY 7 AND U VALUE 6, I WANTED THE JUNCTION 'WHAT IS VALID ONLY 7, HAVE 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);
}
}
    
asked by anonymous 11.11.2018 / 04:07

2 answers

1

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

Replace your for with this:

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);
    }
}
    
11.11.2018 / 04:32
2

Just to be a reference, you can also do as you gave the idea of hashing QU because its HashMap is String . It even gets more organized if you have several two-letter combinations to consider.

For this, store QU with the desired 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 need to first know if you still have a letter forwards. If you have, construct a String with the two current letters and see if it exists in the hash, and only if it does not exist will you only use the punctuation of a 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;
}

See it working on Ideone

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

    
11.11.2018 / 16:15