9

I'm pretty sure that my answer to a question is correct, with 77 upvotes. Have retested just now.

Author of a new answer says the accepted answer is wrong, but they won't respond to comments asking to show their justification or to amend their answer. I suspect they have used an online "regex tester" instead of running the java code specified in the question.

I feel that in this case, since the answer specifically says another answer is incorrect (when it isn't) that a downvote and a comment is not enough. What action should I take?

Link:

How to check if a string contains only digits in Java

25
  • 36
    I think a downvote and a comment is satisfactory. I understand you feel challenged, but when it comes to those looking for an answer, your accepted, high-scoring answer will be enough.
    – CubeJockey
    Sep 10, 2015 at 19:11
  • Additionally, it looks like OP was last online 2 days ago, so be patient. They may not sign on every day.
    – CubeJockey
    Sep 10, 2015 at 19:12
  • @Trobbins Okay, thanks. I'll let the downvoting mechanism take it's course, even if it may cause some confused readers to use a little more time to compile and test both answers. Sep 10, 2015 at 19:13
  • 1
    Just get three 20k+ (or whatever it is) to delete. I have. Sep 10, 2015 at 19:18
  • 2
    Well, as a side effect - posting on meta is a good way to get a flood of downvotes (4 in this case). Thanks everyone :) Sep 10, 2015 at 19:52
  • Hah, you're wrong and he's right. Your regex only tests that the string contains a digit whereas the question asks for a regex that determines if the string "contains only digits". You might want to clarify your answer if you have a reason why this isn't correct.
    – user1228
    Sep 10, 2015 at 20:18
  • 2
    @Will I'm sorry, did you miss the + in the regular expression in my answer? Did you test the code? Sep 10, 2015 at 20:21
  • lol, I don't have to test your code, I can read regular expressions. Your regex will match one or more digits in a row. You tell me, would it not find a match in the following string -- "abc123"? It would match "123" certainly. [meh, snip this assertion; I don't know enough about java to make this call] I can tell you for a fact that your regex, in and of itself, can falsely match a negative case. The answer which you complain about correctly starts and ends the regex with the line start and line ending anchors that you fail to have.
    – user1228
    Sep 10, 2015 at 20:26
  • 9
    @Will you've made the same incorrect presumption as the answer I raised issue about. Please read the question again, it states the usage of the regular expression, via the matches(...) method, which if you are unfamiliar with java matches a complete string (e.g. 123 but not abc123). And generally speaking since the question specifies code, I think you really should make the effort to test the code before calling out an answer as wrong. I note specifically that the question says in Java, not in regular expressions. Sep 10, 2015 at 20:30
  • Ya, just ideone'd it. matches method forces a match against the entire string, even if the regex doesn't specify it. The last line of your above comment doesn't make sense, but I cede the point.
    – user1228
    Sep 10, 2015 at 20:33
  • 3
    Both answers are wrong. The question asks to match strings containing only digits. Your answer fails to match the empty string, which (vacuously) contains only digits.
    – dfeuer
    Sep 11, 2015 at 1:00
  • 14
    You could have headed off the confusion by pointing out that matches implicitly anchors to the start and end of the string — even if not needed by the OP, it would help others passing by and trying to learn. Sep 11, 2015 at 1:01
  • 1
    @JonathanLeffler Okay, thanks, I'll add an explanation about how matches works, and a comment that online "regex checkers" might operate differentlly. Sep 11, 2015 at 5:24
  • 2
    @vikingsteve, since the empty string contains only numbers, a correct regex will match it.
    – dfeuer
    Sep 11, 2015 at 7:01
  • 5
    @dfeuer I'm sure that this is not the spirit of the question. Most people/programmers I know would take "Contains only X" to mean "Contains some X, and nothing else" rather than "Doesn't contain anything that isn't X".
    – Benjamin
    Sep 11, 2015 at 9:57

4 Answers 4

46

I think a comment and a downvote is sufficient in this case. Simply running the code in this case will tell anyone who really wants to know which answer is correct. (Unlike, say, a question about a security vulnerability, where people really can be fooled by answers that look correct, but aren't.)

4
  • Okay, the thing however is that the other answer isn't incorrect - it also works for a positive test case. But from how it is written, people who read and use that answer may then incorrectly infer that my answer is incorrect. Sep 10, 2015 at 19:15
  • @vikingsteve If running the code doesn't tell them which answer is correct (or that both are), then the comments can do that. There's really nothing else to do, unless you want to edit your own answer with test cases proving it works. Sep 10, 2015 at 19:17
  • @vikingsteve Worth noting is that it's unlikely anyone who would assume your answer is incorrect just from that claim probably won't be thorough enough to read past the first two highly upvoted answers, anyway. Or probably wouldn't have been willing to read your entire answer to begin with (since it's a little long). You can't make everyone happy. Just focus on providing high quality content and being humble enough to accept criticism and correction. Don't worry about every last passerby. You'll drive yourself nuts that way. ;)
    – jpmc26
    Sep 11, 2015 at 17:06
  • If the answer simply states that your own answer is incorrect, but it then goes on to provide its own answer, I'd simply edit it to remove the 'fluff'.
    – AStopher
    Sep 13, 2015 at 11:13
10

Unfortunately I do not have enough reputation to post a comment. So I will just add an answer here.

I looked through all comments (and tested the actual use case) and looks like I was wrong that @vikingsteve answer is incorrect.

Do not have any objections about deletion of my answer.

2
  • 2
    No worries. I've had to delete my own answer plenty of times when I misread the question :) The bonus is you get the rep back if it had negative downvotes. Sep 13, 2015 at 10:35
  • 4
    I appreciate you as a new user come to Meta and constructively contribute to the discussion. +1 Sep 13, 2015 at 10:40
4

I think the accepted answer here is incorrect and will fool everyone who will come here for the answer.

1
  • 37
    Brilliant, brilliant.
    – dfeuer
    Sep 11, 2015 at 1:02
-3

This conflict comes from misunderstanding how the pattern with be used to check the string. If \d+ or [0-9]+ is used with find(), it will, indeed, match any substring, regardless of the characters around. In this case we need to add the boundary markers, ^ and $.

However the correct way to use these patterns is by calling matches() instead. Then it would work, and the answer would be correct.

Hence the correct pattern string belongs from the used Java code.

Such problems come from the lack of attention when reading the others question, answer or comment, also the lack of the necessary respect. We should discuss the disagreements quietly and come to the consensus, rather than calling all hell on the opponents head.

In this particular case it is not possible to tell how the pattern string would be used. The Java code in the question is not correct enough to understand this. Hence the correct answer should also include the fragment of Java code, using the proposed pattern.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .