Nizan
Nizan

Reputation: 45

How to return a new number by method

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);
            }
        }
    }
}

Upvotes: 1

Views: 220

Answers (3)

dansalmo
dansalmo

Reputation: 11686

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));

Upvotes: -1

Freaky Thommi
Freaky Thommi

Reputation: 746

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;
    }

Upvotes: 0

DWilches
DWilches

Reputation: 23015

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.

Upvotes: 2

Related Questions