Day 16: Exceptions - String to Integer

Sort by

recency

|

983 Discussions

|

  • + 0 comments

    import Foundation

    /* * Define an ErrorType */ enum StringToIntTypecastingError: Error { case BadString }

    func stringToInt(inputString: String) throws -> Int {
        guard let int = Int(inputString) else {
            throw StringToIntTypecastingError.BadString
        }
        return int
    }
    

    /* * Read the input */ let inputString = readLine()!

    do { try print(stringToInt(inputString: inputString)) } catch StringToIntTypecastingError.BadString { print("Bad String") }

    **// I am getting Error reading result file.You should use exception handling concepts.

    but output printing correct can any one please help in this in Swift language **

  • + 0 comments

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        String S = bufferedReader.readLine();
        try
        {
            System.out.println(Integer.parseInt(S));
        }
        catch(Exception ex)
        {
            System.out.println("Bad String");
        }
        bufferedReader.close();
    }
    

    }

  • + 1 comment

    Where is the d*mn error here:

    using System.CodeDom.Compiler;
    using System.Collections.Generic;
    using System.Collections;
    using System.ComponentModel;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Reflection;
    using System.Runtime.Serialization;
    using System.Text.RegularExpressions;
    using System.Text;
    using System;
    
    
    
    class Solution
    {
        
        public static void Main(string[] args)
        {
            try {
                List<string> numsInInput = new List<string>();
            
                string S = Console.ReadLine();
                
                int num = Int32.Parse(S); // try to convert the string to int
                
                for(int i = 0; i < S.Length ; i++) {
                    numsInInput.Add(S.Substring(i, 1));
                }
                
                if (numsInInput.Count() > 0) {
                foreach (string s in numsInInput) {
                    Console.Write(s);
                }    
                }
            } catch (Exception ex) {
                Console.WriteLine("Bad String");
            } finally {
                //
            }
            
            //FindTheNumbersInInput(S, ref numsInInput);
            /*if (numsInInput.Count() > 0) {
                foreach (string s in numsInInput) {
                    Console.Write(s);
                }
            }*/
        }
    }
    		
    		
    		
    	
    	
    
  • + 6 comments

    For Python3 Remove "if name =='main'; before implementing

    S = input()
    try:
        print(int(S))
    except ValueError:
        print("Bad String")
    
  • + 3 comments

    Python is not checkable i think. Some file read error. Is this code OK?

    if __name__ == '__main__':
        S = input()
        if S.isnumeric():
            print(int(S))
        else:
            print("Bad String")