Day 16: Exceptions - String to Integer

Sort by

recency

|

1012 Discussions

|

  • + 0 comments

    If you get this in your Python 3 code test: Compiler Message: Error reading result file.You should use exception handling concepts

    then remove this line and test again:

    if name == "__main__":
    

    I also stucked in loop to testmy code everywhere, even GPT says my code is correct. I go through discussions and saw same probloem with a guy and tested without that block, I was shocked, my code is now works well and ready for submission.

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    
    S = input().strip()
    try:
        num = int(S)
        print(num)
    except ValueError:
        print("Bad String")
    
  • + 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")