0

So I have been given a practice question that asks to write a method that compares the String verificationCode to the String secretKey character buy character. The int claimValue must also match. It should return true if the characters match and false if they don't.

The code compiles fine. But no matter what tests I run it always returns false. Could someone guide me where I'm going wrong?

public boolean verifyWin(int claimValue, String verificationCode, String secretKey) {
    int prize = 0;
    for (int i = 1; i <= verificationCode.length(); i++) {
        if (verificationCode.charAt(i - 1) == secretKey.charAt(i - 1)) {
            prize = prize + 1;
        } else {
            prize = prize - 1;
        }
    }
    if (prize == claimValue) {
        return true;
    } else {
        return false;
    }
}

The tests are:

 //Test (1):

Lottery winner = new Lottery();

winner.setVerificationCode("HHHHHH");
winner.setSecretKey("HHHHHH");
winner.setClaimValue(1);

winner.verifyWin(1 , "HHHHHH", "HHHHHH");

returns false

//Test 2

Lottery winner = new Lottery();

winner.setVerificationCode("HHHH44");
winner.setSecretKey("HHHH44");
winner.setClaimValue(1);

winner.verifyWin(1 , "HHHH44", "HHHHH44");

returns false
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • 2
    "But no matter what tests I run" please show some of these tests. – Andy Turner Nov 26 '19 at 19:42
  • 1
    please [edit] rather than adding code in comments. – Andy Turner Nov 26 '19 at 19:46
  • 2
    But why would you expect that to return true? There are 6 equal characters, so prize would be 6, not 1. – Andy Turner Nov 26 '19 at 19:47
  • When i create an object and use the method to set the parameters of the int and String arguments to the same characters they always return false. I'm trying to figure out how to to get them both to compare and return true. – Stephen Dynes Nov 26 '19 at 19:54
  • So how do I change the code to check for 6? – Stephen Dynes Nov 26 '19 at 19:55
  • 3
    your code doesn't show creating an object and using the method to set the parameters. Please [edit] to show what you are doing. The content of your deleted comment was fine, but it should have been edited into the question to make it readable. – Andy Turner Nov 26 '19 at 19:55
  • Sorry, this is the first time I have used stack overflow and am getting used to it. My tests are up now. – Stephen Dynes Nov 26 '19 at 20:07
  • `winner.verifyWin(1 , "HHHHHH", "HHHHHH");` returns false, because you pass 1 for `claimValue`, whereas `prize` will be 6. Change 1 to 6, it will return true. – Andy Turner Nov 26 '19 at 20:08
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173) – Andy Turner Nov 26 '19 at 20:09
  • Ah I see now. Simple mistake. Thanks for pointing it out and your help Andy! – Stephen Dynes Nov 26 '19 at 20:20

1 Answers1

0

I would need to know exactly what tests you are running, because when I copypasta your code I get true when I expect to, I get false when I expect to and I get an index out of bounds error when I expected to with your code.

System.out.println(verifyWin(4, "test", "test")); // returns true
System.out.println(verifyWin(6, "test", "test")); // returns false
System.out.println(verifyWin(6, "test", "tester")); // returns false
System.out.println(verifyWin(6, "tester", "test")); // throws index out of bounds error

**Edit: I finished this post after your test cases posted, the issue is the value that you are assigning for your claimValue. Your code will only return true if the claimValue variable is set to the number of characters your string should be.

i.e.

winner.verifyWin(1 , "HHHHHH", "HHHHHH"); // returns false
winner.verifyWin(6 , "HHHHHH", "HHHHHH"); // returns true
KNPS
  • 16
  • 3