-4

I have upload this code already but it was having some problems and now I have update my code this program does not provide me exact reverse number when I enter 123 it return me 321 but if I enter 001 or 100 it just return me 1 in both case help me to solve this issue

public class Employee {
    void fun(int choice) {
        int number = choice;
        int remander = 0;
        int reverse = 0;
        while (number >= 1) {
            remander = number % 10;> taking remainder here

            reverse = reverse * 10 + remander;
            number = number / 10;
        }
        System.out.println(reverse);
    }

    public static void main(String args[]) {
    Employee ob=new Employee();
    int choice;
        System.out.println("Enter number you want to return");
        Scanner obj=new Scanner(System.in);
       choice= obj.nextInt();
    ob.fun(choice);
    }
}
`
Noman Hassan
  • 239
  • 2
  • 10

3 Answers3

0

Here's a one-liner you can use to convert int to Stream of String numbers, reverse it and return as a String:

Stream.of(Integer.toString(choice).split("")).reduce((c1,c2)->c2+c1).get();
wisnix
  • 180
  • 1
  • 8
0

First point: be careful about adding leading 0s to the number. If the number is an integer literal, the leading 0s will actually cause the integer to be interpreted as octal. In this case, 001 octal does, in fact, equal 1 in decimal too, but only due to luck. If you picked another number, it would not give you the result you're expecting; for example, the following code actually prints 63 (not 77):

public class HelloWorld{

     public static void main(String []args){
         int x = 0077;
        System.out.println(x);
     }
}

On the other hand, if you're parsing an integer out of a string or using a scanner, the leading 0s will simply be stripped off. For example, the following prints 77:

System.out.println(Integer.parseInt("0077"));

Unfortunately, in neither case will you get 0077, so the leading 0s won't make any difference when you go to do the reverse operation.

Also, think about what happens for 100:

reverse = reverse * 10 + remander;

Reverse is 0 to start with (and 0 * 10 == 0) and 100 % 10 == 0 because 100 is evenly divisible by 10. Put another way, the statement is equal to:

reverse = 0 * 10 + 0;

which clearly equals 0.

  • A number with a leading zero is only interpreted as octal if it is a literal inside Java code. If the number is input with a Scanner, it is just a number with a leading zero. – NomadMaker Sep 21 '20 at 19:14
0

You can't do this using Scanner.nextInt() because there is no way to tell if leading zeroes were included once they are converted to an int. And the octal situation is not relevant since Scanner does not process ints that way. So you use either an all String solution and verify the characters are digits or use a hybrid of both math and String methods. I did the latter. The following:

  • reads in a String
  • converts to an int
  • calculates the number of leading zeroes by using the log10 of the
    int and the length of the entered string.
  • Allocates a StringBuilder to hold the result.
Scanner input = new Scanner(System.in);

String val = input.nextLine();
int numb = Integer.valueOf(val);
int len = val.length();

// exponent of the entered number rounded up to the next int.
// This computes the number of digits in the actual int value.
int exp = (int) Math.log10(numb) + 1;

// now reverse the integer and store in the StringBuilder
StringBuilder sb = new StringBuilder();
while (numb > 0) {
    sb.append(numb % 10);
    numb /= 10;
}
// and append the leading zeros.
System.out.println(sb + "0".repeat(len - exp));

for input of 00001000
prints 00010000

Note that you are still constrained on input by Integer.MAX_VALUE.

WJS
  • 36,363
  • 4
  • 24
  • 39
  • you mean to say i should take input as String so this issue will resolve cause if i take int then octal situation will occur – Noman Hassan Sep 22 '20 at 14:32
  • Yes. That way the leading zeroes will not imply octal. It also allows you to compute how many leading zeroes there were. – WJS Sep 22 '20 at 14:36