user1649307
user1649307

Reputation: 61

Capturing part of string from a txt file?

I have a text file like this, separated by ";" 1022-3, 1603-4, 2012-5, 2489-6;

Gotta catch the first part before the "-" and pass to variable, and compare with milliseconds, if is equal the number, capture the number after of "-". And do so with the next number after the semicolon, and so front.

public static long MilliSeconds() {
    // get Calendar instance
    Calendar now = Calendar.getInstance();

    return now.getTimeInMillis();

}

And the beginning of the code to do what I need this here

private void LerArquivo() {
        String lstrNomeArq;
        File arq;
        String lstrlinha;
        long tempoInicio = 0;
        long tempoDecorrido = 0;
        try {

            tempoDecorrido = (RecordSound.MilliSeconds() - tempoInicio); 


            lstrNomeArq = "/Android/data/br.com.couldsys.drumspro/cache/GravaSound.TXT";

            String conteudotexto = "";

            arq = new File(Environment.getExternalStorageDirectory(),
                    lstrNomeArq);
            BufferedReader br = new BufferedReader(new FileReader(arq));

            // pega o conteudo do arquivo texto
            conteudotexto = br.readLine();

            String capturaIndex = ("Conteudo do texto: "
                    + conteudotexto.substring(
                            conteudotexto.indexOf("-") + 1,
                            conteudotexto.indexOf(";",
                                    conteudotexto.lastIndexOf("-"))));


            if (tempoDecorrido == capturatempo) { 

                DrumsProActivity.vsm.playSound(capturaindex);
                // ler a nova linha
                // se chegar ao final do string então para o while
                if (conteudotexto.length() > 0) {
                    executar = false;
                }

            }

        } catch (Exception e) {
            trace("Erro : " + e.getMessage());
        }
    }

Upvotes: 3

Views: 160

Answers (3)

user1649307
user1649307

Reputation: 61

What I need now is know how to catch part of text.

arq = new File(Environment.getExternalStorageDirectory(),
                    lstrNomeArq);
            BufferedReader br = new BufferedReader(new FileReader(arq));

            // pega o conteudo do arquivo texto
            conteudotexto = br.readLine(); 

The text may vary more separation has two, the first number is separated from the second number by a "-" (dash) which is then separated by "," (comma)

549-8,1019-9,1404-3,1764-3,2208-10,2593-5,2938-9,3264-6,3700-0,4174-7,4585-8,4840-2,5192-9,5540-10,5932-0,

As has been shown

String[] parte1 = conteudotexto.split(",");

e em seguida

String[] parte2 = parte1[0].split("-");

The rule I'm trying to do is: Turn within a millisecond while the method and compare with the first part of the text

type

**If valor_milissegundos first part is equal to the number of text then ---> enters and runs the function playSound (the second number of the text); ------> goes to the next number in the text loop, capturing the second number of the text and compares it to the millisecond, if equal enters the IF block and catch the second number after the dash, and so on, until you reach the end of the text.**

Method return milliseconds calculates milliseconds

public static long MilliSeconds() {
    // get Calendar instance
    Calendar now = Calendar.getInstance();

    return now.getTimeInMillis();

}

I hope understand why I used the google translator

thank you

Upvotes: 0

dorjeduck
dorjeduck

Reputation: 7804

Maybe not particular elegant but just pragmatic for me - use the String split method. First split with ","

String[] parts = conteudotexto.split(",");

and then with each of the parts (here for the first)

String[] subParts = parts[0].split("-");

Just gives you everything in the pieces you need to look at and no danger get mixed up with positions etc.

Upvotes: 2

ali motamedi
ali motamedi

Reputation: 103

use this simpler code : create an array of substrings each contain a string formated ####-#

string[] MyStr = conteudotexto.split(',');

string sss= MyStr[0];
string sss2= MyStr[1];

....

now sss is 1022-3 sss2 is 1603-4 and so on ....

then reuse split function:

string[] MyStr2 = sss.split('-');

now we have : MyStr2[0] = 1022 MyStr2[1] = 3

Upvotes: 2

Related Questions