Exception String to Integer problem solution 30 days of code HackerRank

Input Format
A single string, .
Constraints
  • , where  is the length of string .
  •  is composed of either lowercase letters (or decimal digits ().
Output Format
Print the parsed integer value of , or Bad String if  cannot be converted to an integer.
Exception String to Integer problem solution 30 days of code HackerRank

Problem solution in c++ programming language.

function main() {
    const S = readLine();
    
    try {
        const N = Number(S);
        isNaN(N) && not_defined_func();
        
        console.log(N);
    } catch (err) {
        console.log('Bad String');
    }
}


Problem solution in Java programming language.

public class Solution {

    public static void main(String[] args) {
        try(Scanner scan = new Scanner(System.in);){
            System.out.println( Integer.parseInt(scan.nextLine()) );
        }
        catch(NumberFormatException e){
            System.out.println("Bad String");
        }
    }
}

Problem solution in Python programming language.

import sys


S = input().strip()

try:
    S = int(S)
    print(S)
except:
    print("Bad String")

Post a Comment

0 Comments