Created
July 28, 2018 23:46
-
-
Save AbdullahMagat/95fbd12c304f9539fed8082a73360e4c to your computer and use it in GitHub Desktop.
Hackerrank Java Int to String Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
//String s=???; Complete this line below | |
// There are many ways to solve this question. here are some. | |
// 1.Solution | |
String s = String.valueOf(n); | |
// 2. Solution | |
String s = Integer.toString(n); | |
// 3. Solution | |
// Stack stc = new Stack(); | |
// int count=0; | |
// while(n!=0){ | |
// stc.add(n%10); | |
// n=n/10; | |
// count++; | |
// } | |
// StringBuilder sb = new StringBuilder(); | |
// for(int i=0; i<count; i++){ | |
// sb.append(stc.pop()); | |
// } | |
// String s=sb.toString(); | |
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); | |
} | |
} | |
Error in main java.lang.
and make a proper java file for code.
java Int to string.java
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable s