Java: Transform a given integer to String format
Java Basic: Exercise-166 with Solution
Write a Java program to transform a given integer into String format.
Pictorial Presentation:

Sample Solution:
Java Code:
import java.util.*;
public class Solution {
public static String transform_int_to_string(int n) {
boolean is_negative = false;
StringBuilder tsb = new StringBuilder();
if (n == 0) {
return "0";
} else if (n < 0) {
is_negative = true;
}
n = Math.abs(n);
while (n > 0) {
tsb.append(n % 10);
n /= 10;
}
if (is_negative) {
tsb.append("-");
}
return tsb.reverse().toString();
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input an integer: ");
int n = in.nextInt();
System.out.println("String format of the said integer: " + transform_int_to_string(n));
}
}
Sample Output:
Input an integer: 35 String format of the said integer: 35
Flowchart:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to move every positive number to the right and every negative number to the left of a given array of integers.
Next: Write a Java program to move every zero to the right side of a given array of integers.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join