Java Exercises: Transform a given integer to String format
Java Basic: Exercise-166 with Solution
Write a Java program to transform a given integer to 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.
Java: Tips of the Day
Java: Anagrams
Generates all anagrams of a string.
public static List<String> anagrams(String input) { if (input.length() <= 2) { return input.length() == 2 ? Arrays.asList(input, input.substring(1) + input.substring(0, 1)) : Collections.singletonList(input); } return IntStream.range(0, input.length()) .mapToObj(i -> new SimpleEntry<>(i, input.substring(i, i + 1))) .flatMap(entry -> anagrams(input.substring(0, entry.getKey()) + input.substring(entry.getKey() + 1)) .stream() .map(s -> entry.getValue() + s)) .collect(Collectors.toList()); }
Ref: https://bit.ly/3rvAdAK
- Weekly Trends
- 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
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises