Day 16: Exceptions - String to Integer

Sort by

recency

|

1011 Discussions

|

  • + 0 comments

    My JS solution:

    const S = readLine();
        
        try {
            Number.isInteger(Number(S)) || (() => { throw new Error(); })();
            console.log(S);
        } catch {
            console.log("Bad String");
        }
    
  • + 0 comments

    string S = Console.ReadLine();

    Console.WriteLine(S.Any(c=> !(c >= 48 && c <= 57) || c==' ')?"Bad String":S);

                                                 or
    

    Console.WriteLine(Int32.TryParse(S, out int result)?result:"Bad String");

  • + 0 comments

    I'm using swift, and getting the same error over and over, did anyone knows how to solve it?

    this is meu received error: Compiler Message Error reading result file.You should use exception handling concepts.

    and this is my working code:

    func stringToInt(inputString: String) throws -> Int {
        guard let value = Int(inputString) else {
            throw StringToIntTypecastingError.BadString
        }
        return value
    }
    
  • + 1 comment

    I had to remove the "if name == 'main':"

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    S = input()
    try: 
        value = int(S) 
        print(value) 
    except ValueError: 
        print("Bad String")
    
  • + 1 comment

    !/bin/python3

    import math import os import random import re import sys if name == 'main': try: S = input() result = int(S) print(result) except ValueError: print("Bad String") except Exception: print("Bad String")