Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AbdullahMagat/95fbd12c304f9539fed8082a73360e4c to your computer and use it in GitHub Desktop.
Save AbdullahMagat/95fbd12c304f9539fed8082a73360e4c to your computer and use it in GitHub Desktop.
Hackerrank Java Int to String Solution
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);
}
}
@surajsahani
Copy link

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable s

at HelloJava/hackerRank.HackedTerminal.main(HackedTerminal.java:20)

@Tanmaybhujade
Copy link

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