Home > Software engineering >  Variable not updating in Java
Variable not updating in Java

Time:04-08

I'm writing a program in which the user enters n number of numbers, the program finds the sum of the digits of the entered numbers, and then prints the number which has the largest sum of digits. For example, n=3, and the entered numbers are 325, 800, 199, then the program should print 199, as 1 9 9 = 19, which is the largest among 800 and 325.

'''

import java.util.Scanner;

public class maxi {
    public static void main(String[] args) {
        Scanner f = new Scanner(System.in);
        System.out.println("Enter n: ");
        int n = f.nextInt();
        int max = 0;
        int c = 0;
        for (int i=0; i<n; i  ) {
            System.out.println("Enter a number: ");
            int a = f.nextInt();
            int e = 0;
            while (a>0) {
                int d = a;
                e  = d;
                a = a/10;
            }
            if (e>c) {
                c = e;
                max = a;
            }
        }
        System.out.println(max);
    }
}

'''

The problem I'm facing is that the variable max is not being updated. I tried printing e (sum of digits) and c (largest sum of digits) inside the for loop, and they are working fine, c is being updated as it should. But max isn't.

CodePudding user response:

Max is being updated. You have max = a;, but at this point a is already zero. This loop:

while (a>0) {
  int d = a;
  e  = d;
  a = a/10;
}

will keep looping until a becomes 0 or less, that's what the condition a>0 means. When max = a; is reached, the only possible value for a is zero. Btw learn to use the debugger, it's your friend.

CodePudding user response:

Unexpected solution is caused by this line

a = a/10;

At some point it will produce zero. Which is exactly the condition for exiting while loop. So you got out while loop and a is equal to 0, then you assign max to 0.

My suggestion is to give more descriptive names for variables and try to debug it one way or another.

Follow notation for class names - upper CamelCase according to the notation.

I reworked your example, it is one of many possible solutions

public class Example {

  private static final Scanner scanner = new Scanner(System.in);

  public static void main(String[] args) {
    System.out.println("Enter n: ");
    int numberOfSamples = scanner.nextInt();
    int result = 0;
    int resultDigitSum = 0;
    for (int i = 0; i < numberOfSamples; i  ) {
      System.out.println("Enter a number: ");
      int inputNumber = scanner.nextInt();
      int quotient = inputNumber;
      int digitSum = 0;
      do {
        digitSum  = quotient % 10;
        quotient = quotient / 10;
      }while(quotient > 0);

      if (digitSum > resultDigitSum) {
        resultDigitSum = digitSum;
        result = inputNumber;
      }
    }
    System.out.println(result);
  }


}

Keep in mind, it would be nice to validate that input integers are positive.

  • Related