Q: A method to add a digit to an existing number at certain index I have a  question in my homework that wants me to create a method that will have 3 parameters:


*

*int number (random number)

*int digit (a digit to add to the number)

*int index (adding the number at this specified index, so the last number will be 0, the next 1 etc)


Now we just started to learn Java and we are not allowed to use any of the magic methods they supply , only the basic operations..and all the loops and conditions of course..
I did create a method to count the numbers but my problem is adding the digit to the number.
public void insertDigit (int number, int digit, int index) {

    int newNumber;
    int numberOfDigits = 0;
    int temp = 0;

    while (number != 0) {

        temp = number % 10;
        number /= 10;
        numberOfDigits++;
    }


}

For example: someObject.insertDigit(345, 5, 1) - this is the input
output - 3145

A: Since this is a learning exercise, I will briefly explain the math behind the problem; you will have to write all your code to satisfy the assignment.
In order to complete your assignment, you would need to do two things:


*

*Chop off the last k digits from a number - given a number N of m digits and a small number k, produce two numbers, A and B, such that A has m-k digits from the front of the number N, and B has the last k digits

*Combine two numbers into one by appending the second number to the first one - Given two numbers A and B, make a third number N by appending the digits of B behind the digits of A.


Given solutions to (1) and (2), you should be able to solve your problem by first chopping off the number N into A and B at the position index, appending the required digit D to the number A, producing AD, and finally appending B to AD for the final result of ADB.
Here is how you solve #1: construct a power of ten equal to the index k at which you want to cut your number, and compute
int pow10k = (int)Math.pow(10, k); // If using Math is not allowed, compute pow(10,k) with a simple loop
int A = N / pow10k;
int B = N % pow10k;

Here is how you solve #2: count the number of digits k in the number being appended (your post shows an almost working way of doing it), then compute the corresponding power of ten. Do the result computation as follows:
// Append B to A
int k = numDigits(B);
int pow10k = (int)Math.pow(10, k);
int AB = A*pow10k + B;

