How can I compare an answer key to answers in java?

402 Views Asked by At

For this project I have to have the user input an answer key and compare it to answers that have been made. For example the answer key would be inputted by the user for each question and the answers would look something like the following: 1231231412, where true = 1, false = 2, a = 1, b = 2, c = 3, etc. I am wondering on how to compare each value to the respective one in the answer key and keep count of the correct ones. I am relatively new to coding in java. Thank you in advance.

1

There are 1 best solutions below

0
Lukas Bauer On

Simply convert the solution into the same format and then compare it with this method. Returns -1, if the input is not of equal length, otherwise the number of right ones.

static int compareKeys(String userInput, String solutionKey){
    if(userInput.length()!=solutionKey.length()) return -1;
    int re=0;
    for (int i = 0; i < userInput.length(); i++) {
        if(userInput.charAt(i)==solutionKey.charAt(i)) re++;
    }
    return  re;
}