Skip to content

Commit 31b9fe9

Browse files
committed
Create IntToString.java
1 parent 76a3c05 commit 31b9fe9

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Java/Introduction/IntToString.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
You are given an integer nn, you have to convert it into a string.
3+
4+
Please complete the partially completed code in the editor. If your code successfully converts nn into a string ss the code will print "Good job". Otherwise it will print "Wrong answer".
5+
6+
nn can range between −100−100 to 100100 inclusive.
7+
*/
8+
9+
import java.util.*;
10+
import java.security.*;
11+
public class IntToString {
12+
public static void main(String[] args) {
13+
14+
Do_Not_Terminate.forbidExit();
15+
16+
try{
17+
Scanner in = new Scanner(System.in);
18+
int n=in.nextInt();
19+
String s = Integer.toString(n);
20+
if(n==Integer.parseInt(s))
21+
{
22+
System.out.println("Good job");
23+
}
24+
else
25+
{
26+
System.out.println("Wrong answer.");
27+
}
28+
}
29+
catch (Do_Not_Terminate.ExitTrappedException e) {
30+
System.out.println("Unsuccessful Termination!!");
31+
}
32+
}
33+
}
34+
35+
//The following class will prevent you from terminating the code using exit(0)!
36+
class Do_Not_Terminate {
37+
38+
public static class ExitTrappedException extends SecurityException {
39+
40+
private static final long serialVersionUID = 1L;
41+
}
42+
43+
public static void forbidExit() {
44+
final SecurityManager securityManager = new SecurityManager() {
45+
@Override
46+
public void checkPermission(Permission permission) {
47+
if (permission.getName().contains("exitVM")) {
48+
throw new ExitTrappedException();
49+
}
50+
}
51+
};
52+
System.setSecurityManager(securityManager);
53+
}
54+
}
55+
56+

0 commit comments

Comments
 (0)