How do I make the next loop perform?

5

I have a list with two or more Strings:

[Panel, Control]

Now comes the problem:

for (int i = 0; i < lista.size(); i++){
    String linha = "";
    while ((linha = leitura.readLine()) != null){
        if (linha.contaens(lista.get(i))){
            System.out.println(lista);

1 - Performs for for with i equal to 0.

2 - Run the while and read row by line by looking for the first string of the list until it reaches null .

3 - The for is called again with i equal to 1.

4 - While it does not execute, leitura.readLine() becomes null .

5 - How do I do while to execute until lista.size() times? Until the list finishes.

In my code it only fetches the first String from the list, but the next one does not execute because the line turned null on the first search.

    
asked by anonymous 25.03.2016 / 07:14

1 answer

4

D3ll4ry,

I would like to understand why you would like to read the same line 2 times to post a response that best fits your situation.

But if you really believe the best way would be to read the same line 2 times, you have to close the file and open it with every for iteration.

for (int i = 0; i < lista.size(); i++){
    /* Abre o arquivo, continue utilizando o que você está usando
       para abrir o arquivo, só coloquei o BufferedReader de exemplo */
    BufferedReader leitura = new BufferedReader(new FileReader('arquivo.txt');
    String linha = "";
    while ((linha = leitura.readLine()) != null){
        if (linha.contaens(lista.get(i))){
            System.out.println(lista);
        }
    }
    leitura.close(); // Fecha o arquivo
}

This happens because you open your file before for , it reads all the lines in your while , but when you return to for , your file has already been completely read.     

25.03.2016 / 07:43