2

I have a file with the lines like this:

ACV01S
ACV02S
ACV03N
ACV03A

And so on (Without the blank lines)

In this part of my code

public static int LerFicheiroLeiParaMatriz(String[][] matrizVotos, String nome) throws FileNotFoundException {

        Scanner finput = new Scanner(new File(nome));
        File f = new File(nome);

        int numeroDeputadosVotos = 0;

        do {
            String temp = finput.nextLine().trim();
            boolean valido = true;
            if (!temp.isEmpty()) {
                matrizVotos[numeroDeputadosVotos][0] = temp.substring(0, 5);
                matrizVotos[numeroDeputadosVotos][1] = temp.substring(5);
                valido = ValidarCodigo(matrizVotos[numeroDeputadosVotos][0]);
                if (valido && matrizVotos[numeroDeputadosVotos][1].length() == 1) {
                    numeroDeputadosVotos++;
                }
            }
        } while (finput.hasNextLine());
        return numeroDeputadosVotos;
    }

The Scanner reads the first line without any problem. The problem is when using the sub string instead of staying like:

ACV01 S

it stays

ACV0 1S

The rest of the file displays normally. Any thoughts?

HRgiger
  • 2,750
  • 26
  • 37
Diogo Santos
  • 780
  • 4
  • 17
  • I just checked System.out.println(" ACV01 S".trim().substring(0, 5)); and it prints correctly...ACV01... and temp.substring(5) prints S.... taking temp = "ACV01 S" – Naruto Dec 04 '15 at 11:33
  • 3
    Are you sure that this is a plain ASCII file? Is the first line probably prefixed with an Unicode BOM marker or similar? If unsure, open the file with a HEX editor and verify it. – Andreas Fester Dec 04 '15 at 11:40
  • Your code is simple enough to easily debug it. Advance line by line and see where you get the wrong behavior. We can help you with understanding *why* something is happening if you tell us *what* is happening. – user1803551 Dec 04 '15 at 11:43
  • The .txt file is set at UTF-8. Is this the problem? – Diogo Santos Dec 04 '15 at 11:44
  • Probably, yes. See http://stackoverflow.com/questions/4897876/reading-utf-8-bom-marker – Andreas Fester Dec 04 '15 at 11:46
  • 1
    Thanks a lot! Working properly now – Diogo Santos Dec 04 '15 at 11:54

0 Answers0