Tuesday, April 23, 2019

How do you convert int to string using java?

Problem statement:
You are given an integer n, you have to convert it into a string.
If your code successfully converts into a string s 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

public class ConvertIntToString {
    public static void main(String[] args) {
        int n = 100;
        //String s = String.valueOf(n);    // method - 1
        //String s = "" + n;                    // method - 2
        String s = Integer.toString(n);     // method - 3
        if (n == Integer.parseInt(s)) {
            System.out.println("Good Job");
        } else {
            System.out.println("Wrong Answer");
        }
    }
}
Output:
Good Job

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...