3
\$\begingroup\$

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 > a1 || 0 > a2 ? "ERROR-NEGATIVE NUMBER" : "OK"));
    while (a1 > 0) {
        sum1 += a1 % 10;
        a1 /= 10;
    }
    //System.out.println(sum1);
    while (a2 > 0) {
        sum2 += a2 % 10;
        a2 /= 10;
    }
    //System.out.println(sum2);
    int temp = sum1 + sum2; //temporary-for storage /=
    while (temp > 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 > sum3) {
            System.out.println(a3 + " this is my number");
            break;
        }
    }

}
}
\$\endgroup\$
  • 1
    \$\begingroup\$ Could you provide some pairs of examples? (input -> expected output) \$\endgroup\$ – akuzminykh Feb 22 at 21:21
  • 1
    \$\begingroup\$ Ex1 : Input: a1 = 222 - so digit sum = 6; a2 = 333 - so digit sum = 9; sum of digits from a1 and a2= 15 = 1 + 5 = 6 so i expect the number with higher digit sum like Output 4545 because 4+5+4+5 = 18 Ex2 Input - a1 - 23234 (digit sum= 14) a2 - 454545(digit sum=27), sum of digits a1+a2 = 14+27 = 41 = 4 + 1 = 5 Output: a3=61= 6+1 = 7 (higher digit sum) \$\endgroup\$ – AdamK Feb 22 at 21:42
  • 1
    \$\begingroup\$ @AdamK I suggest that you edit your answer and add the inputs and outputs, that you gave, in a table / formatted. \$\endgroup\$ – Doi9t Feb 22 at 21:47
3
\$\begingroup\$

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.

  1. 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();
}
  1. 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 > 0) {
      currentSum += userInput % 10;
      userInput /= 10;
   }
   return currentSum;
}

Other observations

  1. 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 > 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 > a1 || 0 > 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 > sum3) {
         return a3;
      }
   }
}

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

private static int askQuestionAndReceiveAnswer(Scanner scn, String s) {
   System.out.println(s);
   return scn.nextInt();
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Welcome to Code Review, you are not closing the Scanner resource and this is a resource leak: to avoid this you can use from java 8 the construct try-with-resource like below:

try (Scanner scn = new Scanner(System.in)) {
    //here your code
}

You can write your line:

System.out.println((0 > a1 || 0 > a2 ? "ERROR-NEGATIVE NUMBER" : "OK"));

using Math.min in the following way:

System.out.println(Math.min(a1, a2) < 0 ? "ERROR-NEGATIVE NUMBER" : "OK");
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.