Results 1 to 4 of 4
  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default reversing digits

    Alright I've got this problem mostly figured out. I'm supposed to take a string of digits and then reverse it. It works except for one thing. If the user inputs 31000, then it should output 13. Likewise, if the user inputs 00013, it should display 31000. However, I come out with 00013 and 31, respectively. how can I remove the leading zeros and keep it from automatically truncating that 00013 to 13?

  2. #2
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Forgot to post my code, here it is.
    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    import java.lang.String;
    
    public class DigitReverse {
    
        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);
        	
    
        }
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by gandalf5166 View Post
    Alright I've got this problem mostly figured out. I'm supposed to take a string of digits and then reverse it. It works except for one thing. If the user inputs 31000, then it should output 13. Likewise, if the user inputs 00013, it should display 31000. However, I come out with 00013 and 31, respectively. how can I remove the leading zeros and keep it from automatically truncating that 00013 to 13?
    Numbers don't have leading zeros; maybe you want to pass them as their String representation instead? (i.e. the input 00013 doesn't exist and can't be passed to your method).

    kind regards,

    Jos

  4. #4
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    That's a good idea, and I could convert the string back to a number after it switched the digits that way it would get rid of the leading zeros from after it reverses 31000.

Similar Threads

  1. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 01:37 PM
  2. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM
  3. Reversing String
    By mew in forum New To Java
    Replies: 4
    Last Post: 12-02-2007, 09:42 PM
  4. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 PM
  5. reversing a string
    By toad in forum New To Java
    Replies: 5
    Last Post: 11-07-2007, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •