Giter Site home page Giter Site logo

Comments (5)

RodneyShag avatar RodneyShag commented on July 30, 2024

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.

from hackerrank_solutions.

wenyanhe-sjsu avatar wenyanhe-sjsu commented on July 30, 2024

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.

from hackerrank_solutions.

RodneyShag avatar RodneyShag commented on July 30, 2024

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.

from hackerrank_solutions.

wenyanhe-sjsu avatar wenyanhe-sjsu commented on July 30, 2024

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.

from hackerrank_solutions.

RodneyShag avatar RodneyShag commented on July 30, 2024

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

from hackerrank_solutions.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.