0

So, we made this in our first semester. Why is it removing the 0 in the end? And how do I fix it? I input 4000 and it showed me 4, when I needed is 4 0 0 0. Same if I input 8030, it outputs 8 0 3.

if (number > 0) {
        while (number != 0) {
        individualNum = individualNum * 10 + number % 10;
        number = number / 10;
        }
        while (individualNum > 0) {
        System.out.print((individualNum % 10));
        sum = sum + (individualNum % 10);
        individualNum = individualNum / 10;
        if (individualNum > 0) {
        System.out.print(" ");
        }
        else if (individualNum < 0){
        System.out.print((individualNum ));
        sum = sum + (individualNum % 10) * -1;
        individualNum = individualNum / 10;
        }
        }
        System.out.println(" = " + sum);
        }
        else if (number < 0){
        while (number != 0) {
        individualNum = individualNum * 10 + number % 10;
        number = number / 10;
        }
        while (individualNum < 0) {
        System.out.print((individualNum*-1 % 10));
        sum = sum + (individualNum % 10);
        individualNum = individualNum / 10;
        if (individualNum < 0) {
        System.out.print(" ");
        }
        else if (individualNum < 0){
        System.out.print((individualNum ));
        sum = sum + (individualNum % 10) ;
        individualNum = individualNum / 10;
        }
        }
        System.out.println(" = " + sum * -1 );
        }
        }catch (InputMismatchException x ){
        System.out.println("Please enter the negative sign at the front");
        }
    }
}
Angel
  • 3
  • 3
  • Just guessing... does it not have to do with your while condition: `while (number != 0)`? – ernest_k Apr 29 '21 at 05:39
  • Did you step through your code with a debugger already? Your conditions all seem to exclude 0 values, i.e. `number != 0` and `individualNum < 0` etc. I also find your code very hard to read due to the missing indentation, please fix this. – Thomas Apr 29 '21 at 05:46

1 Answers1

1

If I understand your code correctly your goal is to list the individual digits and print their sum. To that end, you're first calculating the "inverse" of your input number as individualNum, i.e. if your input was 8030 you'd expect to get 0308. However, note that integers don't have leading zeros so you actually get 308 (or in the case of 4000 just 4).

That's where your trailing zeros are lost.

To fix this you need to use number in your second loop again, but since you've reduced it to 0 in the first loop that doesn't work. Solution? Make a copy first.

if (number > 0) {      ​
 ​//"invert" number
  int n = number;
  while (n != 0) {
    individualNum = individualNum * 10 + n % 10;
    n = n / 10;
  }

  //use number again, i.e. loop until n/10 reaches 0
  n = number;     
  while (n > 0) {
    System.out.print((individualNum % 10));
    sum = sum + (individualNum % 10);
    individualNum = individualNum / 10;
    system.out.print(" ");              
    n /= 10;
  }
  System.out.println(" = " + sum);
}
    

It also looks like you want to tread negative numbers the same, i.e. -8030 should still print 8 0 3 0 = 11. In that case you should negate any negative numbers first, e.g.

if(number < 0 ) {
  number *= -1;
}

Ideally use a copy of number so you can retain your initial input.

Thomas
  • 87,414
  • 12
  • 119
  • 157
  • Good answer +1, but please is it necessary for him to reverse the Integer? I think I'm missing something basic in here. Why not just loop and print the individual values and the sum afterwards? – Mab Apr 29 '21 at 06:40
  • @Mab if the number wasn't reversed first the loop would print `0 3 0 8` due to the `x % 10` (8030 % 10 = 0, 803 % 10 = 3, etc.). He wants to start at the highest digit but he doesn't know what the higest will be so you'd need to iterate back to front. If you do that twice you get it in the correct order. – Thomas Apr 29 '21 at 06:44
  • OK after steering at this for several minutes, if I'm right, the reason the reverse is required is because We also needs to print out each digit. If it's only for the sum ```while(number>0){sum = sum + number%10; number/=10;}``` would do right? – Mab Apr 29 '21 at 07:09
  • 1
    @Mab yes, in that case the order wouldn't matter and a simple loop would suffice. – Thomas Apr 29 '21 at 07:43