Hackerrank - Java Solution - Java Int to String Solution - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Tuesday, December 12, 2017

Hackerrank - Java Solution - Java Int to String Solution

Hackerrank - Java Solution - Java Int to String Solution

HackerRank Problem Solution

Problem Name - Java Int to String
Problem Link -  Java Int to String
Level - Introduction Challenges

Java Only Submit Part Code 
String s = "" + n;


Java Full Source Code 
import java.util.*;
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();
             
            /* Here are 3 ways to convert an int to a String */
            // String s = String.valueOf(n);
            // String s = Integer.toString(n);
            String s = "" + 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);
    }
}


Tags: HackerRank Online Judge Solution, HackerRank OJ Solution list, HackerRank Problems Solution, HackerRank solver, HackerRank all problem solution list, HackerRank solution in java, Hackerrank Java Solution, Hackerrank - Java Solution - Java Int to String Solution, Hackerrank Online Judge Solution, Hackerrank - Java Solutions,

1 comment:

  1. Hi, I made a video with my solution to this problem and my process of solving it. Please leave suggestions in the comments on how I can improve my answer.

    https://www.youtube.com/watch?v=Jb4CBjDqwho

    ReplyDelete