Skip to content

Commit

Permalink
Remove unnecessary boilerplate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
RodneyShag committed Mar 28, 2019
1 parent 6060e5e commit 344d1ae
Showing 1 changed file with 4 additions and 44 deletions.
48 changes: 4 additions & 44 deletions Java/Introduction/Java Int to String/Solution.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,7 @@
// Author: Rodney Shaghoulian
// Github: github.com/RodneyShag

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);
}
}
/* Here are 3 ways to convert an int to a String. Use any of the 3 methods. */
// String s = String.valueOf(n);
// String s = Integer.toString(n);
String s = "" + n;

0 comments on commit 344d1ae

Please sign in to comment.