Compare rows between .txt files

0

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

A prova.txt file has the following content:

1;VVFF

2;VFVF

3;FFFV

The template has the following content:

1;VVFF

2;FFVV

3;FFFF

Make a comparison and add up how many answers matched

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 the same it shows Pontuação = 3

Any ideas?

    
asked by anonymous 25.09.2018 / 19:28

1 answer

0

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 test and feedback responses, so I compared char to char, using the chartAt method of the string class. Note: I did not do 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();
    }

}
    
25.09.2018 / 23:47