1

I'm new in java.
I have to write a program that modifies an existing catalog (3-5 digits) number to a new catalog number by adding a number to the left of an existing catalog number according to these conditions:

  1. The new number will be the largest number between the leftmost digit to the rightmost digit.
  2. If leftmost digit equal to the rightmost, the new number will be 9.

The input should be 10 numbers and then to add a new number. The problem is that, now, the method "newKatalogNumber" get the old catalog number, and return the left digit, i like the method return the new catalog code. for instance, if the method get 1234, she will return 41234, and it will be printed at the end of the main. I have not found a way to do it. Is anybody have the clue how to do that? I will be grateful.

This is my code:

import java.util.Scanner;   // This program gets an old catalog number between 3 to 5 digits and change it to a new catalog number
// by adding a new digit to the left of the number

public class Number
{
    public static int newKatalogNumber(int num)  
    {
        while (num>=10) 
        {
            num /= 10;
        }
       return num;
    }

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

        for (int oldNum=0; oldNum<10; oldNum++)   
        {
            System.out.print("Insert old catalog Number: ");//The user insert the old catalog number
            int catalogNum = input.nextInt();
            int mostRight = catalogNum % 10;   
            int mostLeft = newKatalogNumber(catalogNum);

            //Adding the new digit according to condition below: 

            if (mostRight>mostLeft)
            {
                System.out.println("Old catalog number is:"+catalogNum);
                System.out.println("New catalog number is"+mostRight+catalogNum);
            }
            else if (mostLeft>mostRight)
            {
                System.out.println("Old catalog number is:"+catalogNum);
                System.out.println("New catalog number is"+mostLeft+catalogNum);
            }
            else
            {
                System.out.println("Old catalog number is:"+catalogNum);
                System.out.println("New catalog number is"+9+catalogNum);
            }
        }
    }
}
Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
Nizan
  • 45
  • 1
  • 1
  • 6
  • If your number is 3 and you want to add a 5 on the left, you could do that by adding 50 (50+3 = 53). If your number was 33 you could add 500 (500+33 = 533). If your number was 333 you could add 5000 (5000+333 = 5333). Try to figure out how to generalize that. – hankd Nov 22 '13 at 18:10
  • I think I speak for a lot of folks when I say I don't understand the requirements. Could you illustrate with 2 or 3 manual number transformations? – Gilbert Le Blanc Nov 22 '13 at 18:11

3 Answers3

2

If you have a number like 1234and you want to add at the beginning, let's say, a 5 then you should do:

1234 + 5 * 10000

And if you want to add the 5 at the end you should do:

1234 * 10 + 5 

Notice in the first case the number of zeroes in 10000 equals the number of digits in the original number.

Daniel
  • 21,933
  • 14
  • 72
  • 101
  • Which leaves the harder question of how to get the number of digits. – dansalmo Nov 22 '13 at 18:47
  • 1) 1234 + 5 * 10000 ---> 12345 – Nizan Nov 22 '13 at 18:54
  • @dansalmo jeje, yes, to count the digits one could do integer-division by 10 repeatedly until you get 0. The number of divisions is the number of digits. Or you could just do: `(int)(Math.log10(n)+1)`. BTW: Watch out for `0` ! – Daniel Nov 22 '13 at 22:40
  • The number of digits is easy: Integer.toString(12345).length() – Salandur Nov 23 '13 at 10:21
0

try this

        public static int NewKatatlogNumber (int num)
    {

        int noOfDigitsInTheNumber = num + "".length();
        int[] digitsArray = new int[noOfDigitsInTheNumber];

        int index = digitsArray.length - 1;

        //Extract digit by digit and populate the digitarray from right to left.
        //If your num is 123 your array will [1,2,3]
        while (num > 0)
        {
            int remainder = num % 10;

            num = num / 10;

            digitsArray[index--] = remainder;

        }

        //Find the number to add from the digit array.Apply your logic here
        int numberToAddToTheLeft = getNumberToAdd(digitsArray);


        int newNum = numberToAddToTheLeft;
        //Construct the final token by prepending the digit you identified
        for (int i = 0; i < digitsArray.length; i++)
        {
            int j = digitsArray[i];

            newNum = newNum * 10 + i;

        }

        return newNum;
    }
Freaky Thommi
  • 756
  • 7
  • 18
-1

Convert the number to a string and then add "n" to the front of it. Here is an example using DrJava's interactions tab:

> Integer.parseInt("5" + Integer.toString(500))
5500

> Integer.parseInt(Integer.toString(5) + Integer.toString(500))
5500

But there is a much more succinct (but less efficient) way since adding "" to an int will convert it to a string:

> Integer.parseInt(5 + "" + 500)
5500

If you need to handle negative numbers you can do:

if (n>0)
    System.out.println(Integer.parseInt(p + "" + n));
else
    System.out.println(Integer.parseInt("-" + p + "" + -n));
Community
  • 1
  • 1
dansalmo
  • 11,506
  • 5
  • 58
  • 53