Day 16: Exceptions - String to Integer HackerRank Solution


Day 16: Exceptions - String to Integer HackerRank Solution
Source : https://www.hackerrank.com/challenges/30-exceptions-string-to-integer



Source : https://www.hackerrank.com/challenges/30-exceptions-string-to-integer


Solution


// Karthikalapati.blogspot.com
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.next();
scan.close();
try {
int num = Integer.parseInt(str);
System.out.println(num);
} catch (NumberFormatException e) {
System.out.println("Bad String");
}
}
}

No comments:

Post a Comment