Java Int to String | HackerRank Solution

Hello coders, today we are going to solve Java Int to String HackerRank Solution.

Java Int to String


You are given an integer n, you have to convert it into a string.

Please complete the partially completed code in the editor. If your code successfully converts n into a string s the code will print “Good job”. Otherwise it will print “Wrong answer”.

n can range between -100 to 100 inclusive.

Sample Input


Sample Output

 Good job

Solution – Java Int to String

import java.util.*;
public class Solution {
 public static void main(String[] args) {


  try {
   Scanner in = new Scanner(;
   int n = in .nextInt();
   //String s=???; Complete this line below
   //Write your code here

   String s = "" ;  
   if(n>=-100 && n<=100)
        s = Integer.toString(n);

   if (n == Integer.parseInt(s)) {
    System.out.println("Good job");
   } else {
    System.out.println("Wrong answer.");
  } catch (DoNotTerminate.ExitTrappedException e) {
   System.out.println("Unsuccessful Termination!!");

//The following class will prevent you from terminating the code using exit(0)!
class DoNotTerminate {

 public static class ExitTrappedException extends SecurityException {

  private static final long serialVersionUID = 1;

 public static void forbidExit() {
  final SecurityManager securityManager = new SecurityManager() {
   public void checkPermission(Permission permission) {
    if (permission.getName().contains("exitVM")) {
     throw new ExitTrappedException();

Disclaimer: The above Problem ( Java Int to String ) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.