Day 16: Exceptions – String to Integer In Java | 30 Days Of Code | Hackerrank Programming Solutions

Hello Programmers/Coders, Today we are going to share solutions of Programming problems of 30 Days Of Code, HackerRank. At Each Problem with Successful submission with all Test Cases Passed, you will get an score or marks. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for Day 16: Exceptions – String to Integer in Java-HackerRank Problem. We are providing the correct and tested solutions of coding problems present on HackerRank. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

Link for the ProblemDay 16: Exceptions – String to Integer– Hacker Rank Solution

Day 16: Exceptions - String to Integer – Hacker Rank Solution


Today, we’re getting started with Exceptions by learning how to parse an integer from a string and print a custom error message. Check out the Tutorial tab for learning materials and an instructional video!

Read a string, , and print its integer value; if  cannot be converted to an integer, print Bad String.

Note: You must use the String-to-Integer and exception handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a  score.

Input Format

A single string, .


  • , 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.

Sample Input 0


Sample Output 0


Sample Input 1


Sample Output 1

Bad String


Sample Case  contains an integer, so it should not raise an exception when we attempt to convert it to an integer. Thus, we print the .
Sample Case  does not contain any integers, so an attempt to convert it to an integer will raise an exception. Thus, our exception handler prints Bad String.

Day 16: Exceptions - String to Integer – Hacker Rank Solution
import java.util.Scanner;

 * @author Techno-RJ
public class Day16ExceptionsStringToInteger {
	public static void main(String[] args) {
		Scanner in = new Scanner(;
		String S =;
		int i = 0;
		try {
			i = Integer.parseInt(S);

		} catch (NumberFormatException e) {
			System.out.println("Bad String");

30 thoughts on “Day 16: Exceptions – String to Integer In Java | 30 Days Of Code | Hackerrank Programming Solutions”

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker🙏.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock