简体   繁体   中英

Reversing digits in Java. Leading and trailing zeros won't print

The problem was to reverse user entered digits. I have it working but while testing it I realized that it won't print either leading or trailing zeros.

For example if I enter 10 it only displays 1 in the result.

If I enter 0110 I get a result of 11.

Here is my code:

public class ReversingDigits {

int value;
int reverse;

public ReversingDigits() {
    value = 10;
    reverse = 0;
}// end constructor

public void reverse() {
    System.out.println("Enter a valid 2-4 digit number: ");
    Scanner input = new Scanner(System.in);
    value = input.nextInt();
    if (value < 10 || value > 9999){
        System.out.print("Please enter a valid 2-4 digit number: ");
       value = input.nextInt();
    }

    while (value > 0) {
        reverse *= 10;
        reverse += value % 10;
        value /= 10;

    }
    System.out.println("Reversed numbers are: " + reverse);

}

}//end class

Any ideas on how to get the zeros to print?

Thanks

Make sure you work with a String while reversing your number. It will preserve leading zeros. As you know 00001 is the same as 1 when in int representation, and so converting that to a string will remove all leading zeros.

Here's your code sample modified to read a string from the input, and only convert it to an int when you need to check the range.

public void reverse() {
    System.out.println("Enter a valid 2-4 digit number: ");
    Scanner input = new Scanner(System.in);
    String value = input.next();
    int valueInt = Integer.parseInt(value);

    if (valueInt < 10 || valueInt > 9999){
        System.out.print("Please enter a valid 2-4 digit number: ")
        value = input.next();
    }

    String valueReversed = new StringBuilder(value).reverse().toString();

    System.out.println("Reversed numbers are: " + valueReversed);

}

Note that in your code, if a user enters the wrong range twice in a row, your program won't prompt him again. You may want to put this part of the code into a do-while loop which only exits when the input range is correct. Example

do {
    System.out.print("Please enter a valid 2-4 digit number: ")
    value = input.next();
    int valueInt = Integer.parseInt(value);
} while (valueInt < 10 || valueInt > 9999);
//only get here when inputted value finally within target range.

Edit: As mentioned by @Levenal, you may also want to wrap Integer.parseInt in a try/catch block for NumberFormatException in the event the user passes in a non-numerical input.

As has been pointed out, reversing numbers you are much better off reversing a string. If you are allowed to stray away from console input, JOptionPane is quite good for simple String input, like so:

    while(true){
        String input = JOptionPane.showInputDialog("Please anter a number between 10 & 9999: ");
        if(input == null){//If input cancelled
            break; //Exit loop
        } else if(input.matches("\\d{2,4}")){//Regex for at least 2 but no more than 4 numbers
            System.out.println(new StringBuilder(input).reverse().toString());//Reverse
            break;
        }
    }

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM