0

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;
}
5
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by peter_budo
0

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

0

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

0

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

0

@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

0

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.