Closed wenyanhe-sjsu closed 5 years ago
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.
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.
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.
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.
Scanner's .nextLine() should return NoSuchElementException if no line is found, so I'm not sure why it works on your compiler.
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.