-
Notifications
You must be signed in to change notification settings - Fork 4
/
Java_Int_to_String.java
72 lines (53 loc) · 2.13 KB
/
Java_Int_to_String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
___________________________________________________________Problem Statement____________________________________________________________________________
Problem Statement Link: https://www.hackerrank.com/challenges/java-int-to-string/problem
You are given an integern , you have to convert it into a string.
Please complete the partially completed code in the editor. If your code successfully converts into a string the code will print "Good job". Otherwise it will print "Wrong answer".
n can range between -100 to +100 inclusive.
Sample Input 0
100
Sample Output 0
Good job
________________________________________________________________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 = String.valueOf(n);
// alternative code: String s = Integer.toString(n);
//we can also use this code also: String s = "" + n;
// valueOf is use to converet int into String and parseInt is use for String into int.
// in Java both int and Integer are used to store integer type data the major difference between both is type of int is primitive while Integer is of class type.
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);
}
}