Day 16: Exceptions - String to Integer Hackerrank Solution Java
For Explanation Check Video:
Sample Input 0
3
Sample Output 0
3
Sample Input 1
za
Sample Output 1
Bad String
Code:
import java.io.*;import java.util.*;import java.text.*;import java.math.*;import java.util.regex.*;
public class Solution {
public static void main(String[] args) { Scanner in = new Scanner(System.in); String S = in.next(); try{ int i = Integer.parseInt(S); System.out.println(i); }catch(Exception e){ System.out.println("Bad String"); } }}
Comments
Post a Comment