We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 16: Exceptions - String to Integer
  5. Discussions

Day 16: Exceptions - String to Integer

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 871 Discussions, By:

votes

Please Login in order to post a comment

  • gportela85
    5 years ago+ 13 comments

    A bit ridiculous that with Javascript we are not allowed to use if(isNaN(S) so we have to come up with a hacky code to use some method that will throw errors when passing NaN as param.

    Thought these 30 days of code were supposed to teach people how to program, using the concepts of try/catch/finally and not to write hacky code just to make something work. If this is not ideal for this task, simply remove Javascript as an option for Day 16.

    132|
    Permalink
    View more Comments..
  • harshaktg
    6 years ago+ 18 comments

    those who are looking for a c++ code:

    int main()
    {
        string S;
        cin >> S;
        try
        {
            int no;
            no=stoi(S);
            cout<<no<<endl;
        }
        catch(exception a)
        {
            cout<<"Bad String";
        }
        return 0;
    }
    
    88|
    Permalink
    View more Comments..
  • daniel_stormont
    6 years ago+ 12 comments

    This is a ridiculous exercise! It needs to ignore the comments. I had the code in my Java try/catch block commented and it would fail the test, even though it was obvious the exception was being handled and that the output was correct.

    When I removed the comments, the tests passed. Completely absurd! The test parser should be smart enough to ignore comment lines. Is this really the best you can do HackerRank? I'm really unimpressed.

    38|
    Permalink
    View more Comments..
  • ghost0092
    1 year ago+ 3 comments

    So, I'm doing this:

    s = input()
    try:
        print(int(s))
    except ValueError:
        print("Bad String")
    

    It is literally the way to do it and Hackerank doesn't like it. I guess I'll be forever leaving this problem unsolved....

    27|
    Permalink
  • lugnutz
    6 years ago+ 2 comments

    So it turns out that any mention of the word "if" leads to failure. took me a bit to realise the comment line was the cause...just a little frustrating...

    using System;
    Class Solution {
        static void Main(String[] args)
        {
            string S = Console.ReadLine();
    
            // try and convert, if it fails throw and exception
            try
            {
                Console.WriteLine(int.Parse(S));
            }
            catch (Exception)
            {
                Console.WriteLine("Bad String");
            }
        }
    }
    
    18|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature