Hi. I'm supposed to reverse a number based on user input. Example, if the user inputs 123, the output should be 321. However, so far, the way it is now, if the user inputs 123, the output is 312. I'm a little lost -_- help please.

while ( input != 0 ) {
int num1 = input % 10
int num2 = input / 10;
 //Display results
 JOptionPane.showMessageDialog(null, "Reverse of " + input + " is " + num1 + num2);
10 Years
Discussion Span
Last Post by peter_budo

you could either get all the digits out into a list or something, and then parse them back into a number in reverse

or just take the input as a string and reverse the string


the input will always be a String, as all input from the screen is read as Strings.
And as a single method call will reverse a String, this is really no problem at all :)


enter number: 123

import java.io.*;

public class Reverse {
public static void main(String args[]) throws IOException{

BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
String number = null;
System.out.print("enter number: ");
number = scan.readLine();
StringBuffer temp = new StringBuffer(number);
System.out.print("Reverse number: "+temp.reverse());


@guerreronoli congratulation of you being hyper-active and breaking the fun of the game when Karkalash was supposed to come with solution on his own with little help that he already received... Beside would be nice if you spend sometime reading forums rules, there little section that says For easy readability, always wrap programming code within posts in CODE tags.

Edited by happygeek: fixed formatting


Thanks for the helps and tips :) This was due on Saturday and I was able to figure it out. I can't use a reverse string because we was supposed to use loops only :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.