Problem with hasNext () Java

Posted on

Question :

I have a problem, I do a Scanner to a txt file, then I invoke a method to go to count the lines of this file, after I return the number of lines (to give the size to the String Array equations), I want to go back again go through the file to go to the rest of the problems, but when it goes into while (lerFicheiro.hasNext()) it jumps right out, because hasNext is already false.

Here’s 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 = "";
            valorX1 = "x";
            equacao = valorX1 + valorB + " y_0";
        else if (valorX1.equals("0")){
            equacao = "(" + valorB + "/" + valorX2 + ")";
        }else {
            equacao = "("+ valorX1 + "/" + valorX2 + ")*x+(" + valorB + "/" + valorX2 + ")";
            equacoes[contador] = equacao;
    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("≤")) {
        if (aux.contains("Z")){
    return nFuncoes;

If you can help me thank you


Answer :

The Scanner class does not provide any method that allows you to start.

The only way to do this is to re-create a new Scanner object, 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)));

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


As an alternative to the way you are doing, you can use the Files#readAllLines() that already reads all the lines in the file and returns an object < strong> List<String> . This will dispense with the use of while and you will have to read the file only once, you can later count the rows and obtain each of them using 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 rows, you can use the size() :

int numeroDeLinhas = linhas.size();

If you want to get the contents of a given row, you can use the get() :

String conteudoDaLinhaTres = linhas.get(3);


Leave a Reply

Your email address will not be published. Required fields are marked *