Digit sum loops+random positive number generator I have to prepare program that summing the digits of two given by user numbers and then give back the random number which is 


natural number
the digit sum of this number is bigger then the digit sum of given by user numbers


It's look like everything is ok but i dont know if it is just luck in random number generator or it's working well.
Thank you for review.

import java.util.Random;


import java.util.Scanner;


public class Ex1 {

public static void main(String[] args) {
    Random rnd = new Random();
    Scanner scn = new Scanner(System.in);
    int sum1 = 0;
    int sum2 = 0;
    int sum3 = 0;
    int sum4 = 0;
    System.out.println("1TH NUMBER : ");
    int a1 = scn.nextInt();
    System.out.println("2ND NUMBER : ");
    int a2 = scn.nextInt();
    System.out.println((0 &gt; a1 || 0 &gt; a2 ? "ERROR-NEGATIVE NUMBER" : "OK"));
    while (a1 &gt; 0) {
        sum1 += a1 % 10;
        a1 /= 10;
    }
    //System.out.println(sum1);
    while (a2 &gt; 0) {
        sum2 += a2 % 10;
        a2 /= 10;
    }
    //System.out.println(sum2);
    int temp = sum1 + sum2; //temporary-for storage /=
    while (temp &gt; 0) {
        sum3 += (temp) % 10;
        (temp) /= 10;
    }
    // System.out.println(sum3);
    while (true) {
        int a3 = rnd.nextInt(Integer.MAX_VALUE);
        sum4 += (a3) % 10;
        (a3) /= 10;
       // System.out.println(sum4);
        if (sum4 &gt; sum3) {
            System.out.println(a3 + " this is my number");
            break;
        }
    }

}
}
I have some suggestion for you.

Code duplication

In your code, you have some code duplication that can be extracted in a method. By extracting the code, the code will become shorter, be less error-prone and easier to read.


I suggest that you make a method to print a question and read the user input. 


public static void main(String[] args) {
   //[...]
   int a1 = askQuestionAndReceiveAnswer(scn, "1TH NUMBER : ");
   int a2 = askQuestionAndReceiveAnswer(scn, "2ND NUMBER : ");
   //[...]
}


private static int askQuestionAndReceiveAnswer(Scanner scn, String s) {
   System.out.println(s);
   return scn.nextInt();
}



Since the logic is the same to handle the sum, you can extract both of the while into a method. This extraction will remove lots of code!



public static void main(String[] args) {
   //[...]
   int sum1 = getSum(a1);
   int sum2 = getSum(a2);

   int temp = sum1 + sum2; //temporary-for storage /=

   int sum3 = getSum(temp);
   //[...]
}

private static int getSum(int userInput) {
   int currentSum = 0;
   while (userInput &gt; 0) {
      currentSum += userInput % 10;
      userInput /= 10;
   }
   return currentSum;
}


Other observations


In my opinion, I would extract the last calculation in a method and return the result.


public static void main(String[] args) {
   //[...]
   int number = findNumber(rnd, sum3);
   System.out.println(number + " this is my number");
   //[...]
}

private static int findNumber(Random rnd, int sum3) {
   int sum4 = 0;
   while (true) {
      int a3 = rnd.nextInt(Integer.MAX_VALUE);
      sum4 += (a3) % 10;
      (a3) /= 10;
      if (sum4 &gt; sum3) {
         return a3;
      }
   }
}


Refactored code

public static void main(String[] args) {
   Random rnd = new Random();
   Scanner scn = new Scanner(System.in);

   int a1 = askQuestionAndReceiveAnswer(scn, "1TH NUMBER : ");
   int a2 = askQuestionAndReceiveAnswer(scn, "2ND NUMBER : ");
   System.out.println((0 &gt; a1 || 0 &gt; a2 ? "ERROR-NEGATIVE NUMBER" : "OK"));

   int sum1 = getSum(a1);
   int sum2 = getSum(a2);
   int temp = sum1 + sum2; //temporary-for storage /=

   int sum3 = getSum(temp);

   int number = findNumber(rnd, sum3);
   System.out.println(number + " this is my number");
}

private static int findNumber(Random rnd, int sum3) {
   int sum4 = 0;
   while (true) {
      int a3 = rnd.nextInt(Integer.MAX_VALUE);
      sum4 += (a3) % 10;
      (a3) /= 10;
      if (sum4 &gt; sum3) {
         return a3;
      }
   }
}

private static int getSum(int userInput) {
   int currentSum = 0;
   while (userInput &gt; 0) {
      currentSum += userInput % 10;
      userInput /= 10;
   }
   return currentSum;
}

private static int askQuestionAndReceiveAnswer(Scanner scn, String s) {
   System.out.println(s);
   return scn.nextInt();
}