HackerRank 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".



  1. import java.util.*;
  2. import java.security.*;
  3. public class Solution {
  4. public static void main(String[] args) {
  5. DoNotTerminate.forbidExit();
  6. try {
  7. Scanner in = new Scanner(System.in);
  8. int n = in .nextInt();
  9. in.close();
  10. //String s=???; Complete this line below
  11. String s = "" + n;
  12. if (n == Integer.parseInt(s)) {
  13. System.out.println("Good job");
  14. } else {
  15. System.out.println("Wrong answer.");
  16. }
  17. } catch (DoNotTerminate.ExitTrappedException e) {
  18. System.out.println("Unsuccessful Termination!!");
  19. }
  20. }
  21. }
  22. //The following class will prevent you from terminating the code using exit(0)!
  23. class DoNotTerminate {
  24. public static class ExitTrappedException extends SecurityException {
  25. private static final long serialVersionUID = 1;
  26. }
  27. public static void forbidExit() {
  28. final SecurityManager securityManager = new SecurityManager() {
  29. @Override
  30. public void checkPermission(Permission permission) {
  31. if (permission.getName().contains("exitVM")) {
  32. throw new ExitTrappedException();
  33. }
  34. }
  35. };
  36. System.setSecurityManager(securityManager);
  37. }
  38. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube