Convert to String (with.Java)
This is a post about the āConvert to Stringā problem.
Weāre going to learn about it by solving coding test problems, reflecting on the problems we solved, and exploring other ways to solve them.
Letās start with the problem.
Problem
Complete the solution function so that, given an integer n, it converts n to a string and returns it.
Example input and output
n | result |
---|---|
123 | ā123ā |
2573 | ā2573ā |
My solution to the problem
class Solution {
public String solution(int n) {
String answer = String.valueOf(n);
return answer;
}
}
Explanation of the solution
We used the String.valueOf function, which converts an integer datatype to a string datatype, and returned the value in answer.