Videos
How do you reverse a negative number in Java?
How do you reverse a number in Java without using a loop?
Which approach is faster for reversing a number in Java?
You're storing your reversed number as an int. The reverse of 1200 is 0021, but that's just 21 as an int. You can fix it by converting each digit to a string separately.
The problem is that you're calculating the reversed value as a number and, when it comes to numbers, there is no difference between 0021 and 21. What you want is to either print out the reversed value directly as you're reversing it or build it as a string and then print it out.
The former approach would go like this:
System.out.print("The reversed number is ");
while (num != 0)
{
System.out.print(num % 10);
num = num / 10;
}
System.out.println();
The latter approach would go like this:
String reverse = "";
while (num != 0)
{
reverse = reverse + Integer.toString(reverse);
num = num / 10;
}
System.out.println("The reversed number is " + reverse);
The latter approach is useful if you need to do further work with the reversed value. However, it's suboptimal for reasons that go beyond the scope of this question. You can get more information if you do research about when it's better to use StringBuilder instead of string concatenation.
You only need to iterate across the string halfway, swapping the characters as the indices go towards the middle. This works for even or odd length strings.
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:- ");
char[] chars = sc.next().toCharArray();
int len = chars.length;
for (int i = 0; i < len/2; i++) {
char c1 = chars[i];
char c2 = chars[len-i-1];
chars[i] = c2;
chars[len-i-1] = c1;
}
String s = new String(chars);
System.out.println(s);
Or just use the reverse() method included in the StringBuilder class.
import java.util.Scanner;
public class Demo{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number:- ");
String number = sc.next();
for (int i = number.length() -1 ; i >= 0; i--) { // correct answer: number.lenght() -1 as array start with 0.
System.out.print(number.charAt(i)); //using print instead of println so that it display in same line
}
}
}
First possibility use String
You can use directly the String as your data structure. Adding at the beginning instead of at the end when you read a new number.
Basically with a code like:
String str = "";
while (yourcondition) {
str = scanner.nextInt() + " " + str;
}
return str;
Second possibility use StringBuffer or StringBuilder
StringBuilder str = new StringBuilder();
while (yourcondition) {
str.insert(0, " ").insert(0, scanner.nextInt());
}
return str.toString();
Third possibility use recursion:
public String read(String str, Scanner scanner) {
if (testExitCondition) {
return str;
}
return scanner.nextInt() + " " + str;
}
// Called with System.out.println(read("", scanner));
Note
The first solution use String creating many instances of String, but having only one reference to it.
The second solution use existing classes that internally uses arrays to operate on strings.
The third leave to the JVM the reference to the created String, so no explicit reference to the string is present in the code. This is the only that don't have explicit variable declarations (only in the signature of method, not outside).
You can use scanner.hasNext() with a while loop. Use StringBuilder to collect prepaired data.
Scanner scanner = new Scanner("12 52 10");
StringBuilder builder = new StringBuilder();
while (scanner.hasNext()) {
builder.insert(0, scanner.next()).insert(0, " ");
}
builder.replace(0, 1, "");
System.out.println(builder.toString());