Problem with hasNext() Java

Asked

Viewed 321 times

3

Hello, I have a problem, I scan a txt file, then I invoke a method to go counting the lines of this file, after returning me the number of lines (to give the size to the String Array equations)I want to go through the file again to deal with the rest of the problems, but when you go into the while (lerFicheiro.hasNext()) jump right out, because hasNext is already false.

Here is the code

static  double[][] LerFicheiro () throws FileNotFoundException{
    String ficheiro = "ficheiro_teste.txt";
    Scanner lerFicheiro = new Scanner(new File(ficheiro));
    double[][] matrizValores = new double[1][5];
    String aux;
    int num = 0;
    int contador = 0; // variavel auxiliar para contar a linha que está a ser percorrida
    int numLinhas = numeroLinhasMatriz(lerFicheiro);
    String[] equacoes = new String[num];

    while (lerFicheiro.hasNext()) { //enquanto o ficheiro tiver conteudo, vamos percorrer linha a linha
        aux = lerFicheiro.nextLine();
        if (!aux.isEmpty() && aux.length() > 0) {
            aux = aux.toUpperCase();
            if (aux.contains("Z")){
                aux = lerFicheiro.nextLine();
            }         
        int posX1 = aux.indexOf("X");            
        int posX2 = aux.indexOf("X", posX1+1);
        int posB = aux.indexOf("=");
        String valorX1 = valoresX(posX1, aux); //valor do X1 no ficheiro
        String valorX2 = valoresX(posX2, aux); //valor do X2 no ficheiro
        String valorB = matrizInsereB(posB, aux); //valor do B no ficheiro
        /*System.out.println("x1 " + j);
        System.out.println("x2 " + k);
        System.out.println("b "+ b);*/
        String equacao = "";
        if(valorX1.equals("1")){
            valorX1 = "x";
        }
        if(valorX2.equals("0")){
            equacao = valorX1 + valorB + " y_0";
        }
        else if (valorX1.equals("0")){
            equacao = "(" + valorB + "/" + valorX2 + ")";
        }else {
            equacao = "("+ valorX1 + "/" + valorX2 + ")*x+(" + valorB + "/" + valorX2 + ")";
        }      
            equacoes[contador] = equacao;
            contador++;
        }
    }
    for (int i = 0; i < equacoes.length; i++){
        System.out.println("posicao "+i +" "+equacoes[i]);
    }
    return matrizValores;
}
static int numeroLinhasMatriz(Scanner ler) {
    String aux;
    int nFuncoes = 0; 
    while (ler.hasNext()) {
        aux = ler.nextLine();
        if (aux.contains("=") || aux.contains("≤")) {
            nFuncoes++;
        }
        if (aux.contains("Z")){
            nFuncoes--;
        }
    } 
    return nFuncoes;
}

If you could help me, I’d appreciate it

2 answers

4


The class Scanner does not provide any method to return to the beginning.

The only way to do that is to re-create a new object Scanner, do this after the line int numLinhas = numeroLinhasMatriz(lerFicheiro);

int numLinhas = numeroLinhasMatriz(lerFicheiro);
lerFicheiro = new Scanner(new File(ficheiro));  

Or create a new one by calling the method:

int numLinhas = numeroLinhasMatriz(new Scanner(new File(ficheiro)));

Note:
You are declaring an array with 0 elements.
I judge the line String[] equacoes = new String[num]; should be String[] equacoes = new String[numLinhas];

  • I had already tried that way, but I wanted to find a way not to have to change the file name more than once.

  • See the @Renan response.

2

As an alternative to the way you are doing, you can use the method Files#readAllLines() that already reads all the lines in the file and returns an object List<String>. This will dispense with the use of while and you will need to read the file only once, later you can count the lines and get each of them through the interface methods List .

Path localDoArquivo = Paths.get("D:\\foo.txt");
List<String> linhas = Files.readAllLines(localDoArquivo, StandardCharsets.UTF_8);

If you want to know the number of lines, you can use the method size():

int numeroDeLinhas = linhas.size();

If you want to get the contents of a particular line, you can use the method get():

String conteudoDaLinhaTres = linhas.get(3);

Browser other questions tagged

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