Algorithm


Problem Name: Java Int to String

Problem Link: https://www.hackerrank.com/challenges/java-int-to-string/problem?isFullScreen=true

In this HackerRank Functions in Java programming problem solution,

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 0

100

Sample Output 0

Good job

 

 

Code Examples

#1 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;
import java.security.*;

public class Solution{
	public static void main(String[] args){

		DoNotTerminate.forbidExit();

		try{
			Scanner in = new Scanner(System.in);
			int n = in.nextInt();
			in.close();
			
			String s = String.valueOf(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(){
   		@Override
	   		public void checkPermission(Permission permission){
				if(permission.getName().contains("exitVM")){
		 			throw new ExitTrappedException();
				}
	   		}
  		};
  
		System.setSecurityManager(securityManager);
 	}
}

Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Java Static Initializer Block Java solution in Hackerrank - Hacerrank solution Java
Next
[Solved] Java Date and Time in Java solution in Hackerrank - Hacerrank solution Java