Compare lines between. txt files

Asked

Viewed 576 times

0

Good afternoon, I need to compare two files .txt and check that each character is equal to the other file and store which characters are equal... Type a template

an archive prova.txt has the following content:

1;VVFF

2;VFVF

3;FFFV

The template has the contents:

1;VVFF

2;FFVV

3;FFFF

Make a comparison and add up how many answers they got right

int pontuacao = 0;

Scanner sc = new Scanner(System.in);

String gabaritoVetor[] = new String[3];
String notaVetor[] = new String[3];

for (int c = 0; c < gabaritoVetor.length; c++) {
    gabaritoVetor[c] = leitura; //recebe os dados do prova.txt

}
for (int i = 0; i < notaVetor.length; i++) {
    notaVetor[i] = leitura2; //recebe os dados do gabarito.txt
    if (notaVetor[i].equals(gabaritoVetor[i])) {
        pontuacao++;
    }
}

System.out.println("\nPontuação: " + pontuacao);

I tested with the same files worked, when I modified gave Pontuação = 0 If the files are equal shows Pontuação = 3

Any idea???

  • I think he’s comparing every line in the file, not every character.

1 answer

1

You can use the split method of the String class to clean the file (read more about the method). With the split I was able to isolate only the answers of the proof and jig, so I compared char to char, using the chartAt method of the string class. Note: I did not make the method to read the text file, because I believe that this is not your problem.

package teste2;

import java.util.Scanner;

public class LeitorArquivo {

    public static void main(String[] args) {
        String [] gabarito = new String[3];
        String [] prova = new String[3];
        Scanner sc = new Scanner(System.in);

        int pontuacao = 0;

        for (int i = 0; i < prova.length; i++) {
            System.out.println("Entre com o gabarito da alternativa " + (i+1));
            gabarito[i] = sc.nextLine();
            System.out.println("Entre com as respostas da alternativa " + (i+1));
            prova[i] = sc.nextLine();
        }

        for (int i = 0; i < prova.length; i++) {
            String respostaProva = prova[i].split(";")[1];
            String respostaGabarito = gabarito[i].split(";")[1];

            for (int j = 0; j < respostaProva.length(); j++) {
                if(respostaProva.charAt(j) == respostaGabarito.charAt(j))
                    pontuacao++;
            }
        }

        System.out.println("Pontuação final é: " + pontuacao);

        sc.close();
    }

}

Browser other questions tagged

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