Day 16: Exceptions - String to Integer
Day 16: Exceptions - String to Integer
+ 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.
+ 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; }
+ 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.
+ 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....
+ 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"); } } }
Sort 871 Discussions, By:
Please Login in order to post a comment