Java: Problem with Substring

Asked

Viewed 139 times

-1

I created a program to translate musical notes from a site to a more readable format, it receives and analyzes every two digits of a string looking for them (as key) within a hashmap, if found, the int it returns is used on a switch that will find the corresponding characters that will be written to a new string.

novaString is a class variable, it is used in the public translate method to concatenate the translated characters and is used in the private translate method when switch case 63 is called, in this private method I try to delete the last character of novaString with substring, but it looks like I’m editing a copy and not the variable itself because no changes made to that line are saved.

public class main {

    public static void main(String[] args) {
        Resources r1 = new Resources();
        String texto = "[t8] ttt [r5] t r [r4] t y [u5] y\r\n" + 
                "[t8] ttt [r5] t t [w4] 5\r\n" + 
                "[t8] ttt [r5] t r [r4] t y [u5] y\r\n" + 
                "[t8] ttt [r5] t y [w4] [w5]\r\n" + 
                "[t8] t [y5] w [o4] i u [y5]\r\n" + 
                "u i [u8] y t [r5] t r [e4] [w5]\r\n" + 
                "[t8] t [y5] w [o4] i u [y5]\r\n" + 
                "u i [u8] y t [r5] t r [r4] t y [u5] y [t8]";

        r1.c1.gravar("Titanic", r1.traduzir(texto));

    }

}
import java.util.HashMap;
import java.util.Map;

public class Resources {

    Map<String,Integer> example = new HashMap<String,Integer>();
    public Conectar c1 = new Conectar();

    private String novaString = "";

    public Resources() {
        // TODO Auto-generated constructor stub
        bancoDados();

    }

    private void bancoDados() {
        example.put("1", 1); // dó
        example.put("1|", 2); // dó sustenido
        example.put("2", 3); // ré
        example.put("2|", 4); // ré sustenido
        example.put("3", 5); // mi
        example.put("4", 6); // fá
        example.put("4|", 7); // fá sustenido
        example.put("5", 8); // sol
        example.put("5|", 9); // sol sustenido
        example.put("6", 10); // lá
        example.put("6|", 11); // lá sustenido
        example.put("7", 12); // si

        example.put("8", 13); // dó
        example.put("8|", 14); // dó sustenido
        example.put("9", 15); // ré
        example.put("9|", 16); // ré sustenido
        example.put("0", 17); // mi
        example.put("q", 18); // fá
        example.put("Q", 19); // fá sustenido
        example.put("w", 20); // sol
        example.put("W", 21); // sol sustenido
        example.put("e", 22); // lá
        example.put("E", 23); // lá sustenido
        example.put("r", 24); // si

        example.put("t", 25); // dó
        example.put("T", 26); // dó sustenido
        example.put("y", 27); // ré
        example.put("Y", 28); // ré sustenido
        example.put("u", 29); // mi
        example.put("i", 30); // fá
        example.put("I", 31); // fá sustenido
        example.put("o", 32); // sol
        example.put("O", 33); // sol sustenido
        example.put("p", 34); // lá
        example.put("P", 35); // lá sustenido
        example.put("a", 36); // si

        example.put("s", 37); // dó
        example.put("S", 38); // dó sustenido
        example.put("d", 39); // ré
        example.put("D", 40); // ré sustenido
        example.put("f", 41); // mi
        example.put("g", 42); // fá
        example.put("G", 43); // fá sustenido
        example.put("h", 44); // sol
        example.put("H", 45); // sol sustenido
        example.put("j", 46); // lá
        example.put("J", 47); // lá sustenido
        example.put("k", 48); // si

        example.put("l", 49); // dó
        example.put("L", 50); // dó sustenido
        example.put("z", 51); // ré
        example.put("Z", 52); // ré sustenido
        example.put("x", 53); // mi
        example.put("c", 54); // fá
        example.put("C", 55); // fá sustenido
        example.put("v", 56); // sol
        example.put("V", 57); // sol sustenido
        example.put("b", 58); // lá
        example.put("B", 59); // lá sustenido
        example.put("n", 60); // si

        example.put("m", 61); // dó
        example.put("[", 62);
        example.put("]", 63);

    }

    public String traduzir(String tr) {



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

            String str;

            if(tr.length() >= i + 1) {
                str = tr.substring(0 + i, 1 + i);

                if (example.get(str) == null) {
                    if (tr.length() >= i + 2) {
                        str = tr.substring(0 + i, 1 + i);

                        if (example.get(str) == null) {

                        }                           
                        else
                            novaString += traduzir(example.get(str));
                    }
                }                   
                else {
                    novaString += traduzir(example.get(str));
                }
            }
        }
        return novaString;
    }

    private String traduzir(int i) {
        switch (i) {
            case 1 :
                return "1C "; // dó

            case 2 :
                return "1C# "; // dó sustenido

            case 3 :
                return "1D "; // ré

            case 4 :
                return "1D# "; // ré sustenido

            case 5 :
                return "1E "; // mi

            case 6 :
                return "1F "; // fá

            case 7 :
                return "1F# "; // fá sustenido

            case 8 :
                return "1G "; // sol

            case 9 :
                return "1G# "; // sol sustenido

            case 10 :
                return "1A "; // lá

            case 11 :
                return "1A# "; // lá sustenido

            case 12 :
                return "1B "; // si



            case 13 :
                return "2C "; // dó

            case 14 :
                return "2C# "; // dó sustenido

            case 15 :
                return "2D "; // ré

            case 16 :
                return "2D# "; // ré sustenido

            case 17 :
                return "2E "; // mi

            case 18 :
                return "2F "; // fá

            case 19 :
                return "2F# "; // fá sustenido

            case 20 :
                return "2G "; // sol

            case 21 :
                return "2G# "; // sol sustenido

            case 22 :
                return "2A "; // lá

            case 23 :
                return "2A# "; // lá sustenido

            case 24 :
                return "2B "; // si 


            case 25 :
                return "3C "; // dó

            case 26 :
                return "3C# "; // dó sustenido

            case 27 :
                return "3D "; // ré

            case 28 :
                return "3D# "; // ré sustenido

            case 29 :
                return "3E "; // mi

            case 30 :
                return "3F "; // fá

            case 31 :
                return "3F# "; // fá sustenido

            case 32 :
                return "3G "; // sol

            case 33 :
                return "3G# "; // sol sustenido

            case 34 :
                return "3A "; // lá

            case 35 :
                return "3A# "; // lá sustenido

            case 36 :
                return "3B "; // si 

            case 37 :
                return "4C "; // dó

            case 38 :
                return "4C# "; // dó sustenido

            case 39 :
                return "4D "; // ré

            case 40 :
                return "4D# "; // ré sustenido

            case 41 :
                return "4E "; // mi

            case 42 :
                return "4F "; // fá

            case 43 :
                return "4F# "; // fá sustenido

            case 44 :
                return "4G "; // sol

            case 45 :
                return "4G# "; // sol sustenido

            case 46 :
                return "4A "; // lá

            case 47 :
                return "4A# "; // lá sustenido

            case 48 :
                return "4B "; // si 


            case 49 :
                return "5C "; // dó

            case 50 :
                return "5C# "; // dó sustenido

            case 51 :
                return "5D "; // ré

            case 52 :
                return "5D# "; // ré sustenido

            case 53 :
                return "5E "; // mi

            case 54 :
                return "5F "; // fá

            case 55 :
                return "5F# "; // fá sustenido

            case 56 :
                return "5G "; // sol

            case 57 :
                return "5G# "; // sol sustenido

            case 58 :
                return "5A "; // lá

            case 59 :
                return "5A# "; // lá sustenido

            case 60 :
                return "5B "; // si


            case 61 :
                return "6C "; // dó

            case 62 :
                return "[";

            case 63 :
                novaString = novaString.substring(0, novaString.length() - 1);
                return "] ";

            default:
                return "";
        }
    }
}

All I want is when case 63 is called the newString last position character is deleted.

1 answer

1


You have to remember that String is a collection of characters analogous to the `Array``:

  • As with the Arrays the index of the first character is 0(zero).
  • The index of the last character, if any, is the length of String less 1(a). This is because the index of the first character is 0, second is 1, of the third is 2,..., the index of the nth element(N) is equal to the ordinal position of the nth element minus one, which in its example can be translated with novaString.length() - 1

Possession of information that the index of the last element of a String is length() - 1 to remove the last element of a Java String within the method substring is only to remove one more element from the length of the String:

novaString.length() - 2

So your case 63 can be rewritten as follows:

    case 63 :
        // Verifica o comprimento da string para evitar o erro Index out of range(índice fora da faixa)
        novaString = (novaString.length() >= 2) ?  

                               // Caso comprimento seja maior ou igual a dois retorna o substring sem o último elemento.
                               novaString.substring(0, novaString.length() - 2) : 

                               //Caso o comprimento seja menor que dois retorna um string vazio.
                               "";
        return "] ";
  • This is a mistake, if case 63 were called this error would occur, but that is not what is happening. I noticed that in the logic I used the program is not able to format the text properly to find the terms in the hashmap, apparently only the cases 62 and 63 are not being called. I will have to review the logic of the code to fix the problem.

  • Since you will review the logic of the code consider using the Javacc is an parser generator. The generated parser reads an arbitrary grammar, in your case it can be musical notes, and converts it into Java code.

Browser other questions tagged

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