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);
 break;
}

Recommended Answers

All 7 Replies

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 :)

example:
enter number: 123

code:
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.

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 :)

That something you forgot to mention...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.