Here is the code I have done so far. The problem with this is that it reverse "12300" as "321" only. It ignores the 0's.
Would it be possible to use String.ValueOf() here? Please help!
import java.util.Scanner; // program uses class Scanner
public class ReverseDigits
{
static Scanner console = new Scanner(System.in);
public static void main( String args[])
{
int number;
System.out.println("Enter an integer");
number = console.nextInt();
reverse (number); // print the method reverse
} // end method main
public static void reverse ( int num )
{
int lastDigit; // the last digit returned when reversed
int reverse = 0;
do
{
lastDigit = num % 10;
reverse = (reverse * 10) + lastDigit;
num = num / 10;
}
while (num > 0);
System.out.println("The integer with its digits reversed " + reverse);
}
Help needed in writing a java program to reverse digits!
Started by charlote, May 09 2010 04:17 PM
4 replies to this topic
#1
Posted 09 May 2010 - 04:17 PM
|
|
|
#2
Posted 09 May 2010 - 04:36 PM
You could just turn the integer into a character array, reverse the array, then put it back into a string to print in out like:
public static void reverse(int num){
String a = ""+num;
char[] chars = a.toCharArray();
char[] reversedChars = new char[chars.length];
int placement = 0;
for(int i=chars.length-1; i>=0; i--){
reversedChars[placement] = chars[i];
placement++;
}
String out = String.copyValueOf(reversedChars);
System.out.println("The integer with its digits reversed "+out);
}
twas brillig
#3
Posted 09 May 2010 - 04:53 PM
Thanks. But I haven't yet read or learnt about arrays. Will try. Thanks.
#4
Posted 09 May 2010 - 04:57 PM
Here is a much simpler implementation using String:
import java.util.Scanner; // program uses class Scanner
public class ReverseDigits
{
static Scanner console = new Scanner(System.in);
public static void main( String args[])
{
String number;
System.out.println("Enter an integer");
number = console.nextLine();
reverse (number); // print the method reverse
} // end method main
public static void reverse ( String num )
{
int lastDigit; // the last digit returned when reversed
int reverse = 0;
for(int i = num.length()-1; i >= 0; i--)
{
System.out.print(num.charAt(i));
}
System.out.println(" is integer with its digits reversed.");
}
}
Edited by ZekeDragon, 17 May 2010 - 02:40 AM.
Use [code] tags (the # button) when posting code.
#5
Posted 09 May 2010 - 09:39 PM
Thanks. It is been solved and I tried the code below:
public class ReverseDigit {
public static long reverseDigit(long num) {
long rnum;
rnum = num % 10;
return(rnum);
}
public static void main(String[] args) {
long num;
final int FLAG = 0;
int restart;
String snum;
int length;
long rdigit;
Scanner input = new Scanner(System.in);
do{System.out.print("Enter the number: ");
num = input.nextLong();
snum = Long.toString(num);
length = snum.length();
if (num >= 0) {
System.out.print("The reverse of your number is: ");
for (int i = 1; i <= length; i++) {
rdigit = reverseDigit(num);
System.out.print(rdigit);
num /= 10;
}
} else {
num = Math.abs(num);
System.out.print("The reverse of your number is: -");
for (int i = 1; i < length; i++) {
rdigit = reverseDigit(num);
System.out.print(rdigit);
num /= 10;
}
}
System.out.println();
System.out.print("Enter 0 to quit, any other number to continue: ");
restart = input.nextInt();
}while(restart != FLAG);
}
}
Edited by ZekeDragon, 17 May 2010 - 02:40 AM.
Use [code] tags (the # button) when posting code.


Sign In
Create Account


Back to top









