Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

potential error in your Java String Tokens solution #15

Closed
wenyanhe-sjsu opened this issue Mar 29, 2019 · 5 comments
Closed

potential error in your Java String Tokens solution #15

wenyanhe-sjsu opened this issue Mar 29, 2019 · 5 comments

Comments

@wenyanhe-sjsu
Copy link

Hi,
I like your work on HackerRank problems. But I noticed a potential error in your "Java String Tokens" solution. It passes all test cases provided on HackerRank. But here's one customized test case I came up with: the string is "@@@ I am ok. How about you? ??". Maybe you are interested in looking further into it.

Thank you for your time.

@RodneyShag
Copy link
Owner

Nice test-case. The leading "@@@" in the string seems to break the code. I have updated the solution and it will now work with your testcase. Thanks.

@wenyanhe-sjsu
Copy link
Author

Thank you for taking the time to fix it. Now I have follow-up questions.

I designed a customized test case where the input is three spaces. I tried your updated solution and my own on Hackerrank. Both incurred an error:


Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Solution.main(Solution.java:8)


Line 8 is provided in the original stub code: String s = scan.nextLine();
However, if I run the same program (yours or mine) on my local machine (Ubuntu Linux with openjdk version "1.8.0_191"), there is no error. A customized input of three spaces leads to an output of 0 with no tokens printed, which is what one would expect. So now my question is why would Hackerrank produce such an error for this customized input. Just FYI, an input of null (meaning pressing the return key without entering anything) has the same behavior.

If you have time and are interested in looking into this, I'd like to know what you think of it. Thanks.

@RodneyShag
Copy link
Owner

Good job solving on this extreme edgecase. If your input is just 3 spaces as " ", then scan.nextLine() cannot find a line to return since it skips over all whitespace. In your case you handle it perfectly. Since they don't specifically ask to test for input without letters, and didn't test for it explicitly in their own tests, I'm okay with assuming they intended us to solve the problem for input that always contains letters.

@wenyanhe-sjsu
Copy link
Author

I probably didn't make myself clear enough. My question really is about this. Hackerrank compiler produces an exception for the extreme case (which I agree is probably unintended). But the compiler on my own computer (with openjdk 1.8.0_191) gives no error/exception for the same exact code and produces the expected output. So should I just assume that this discrepancy is caused by use of different Java compilers, which handle situations differently? I don't know much about how Java works under the hood. Hence my latest question.

Thanks again for taking the time and effort responding.

@RodneyShag
Copy link
Owner

Scanner's .nextLine() should return NoSuchElementException if no line is found, so I'm not sure why it works on your compiler.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants