Reputation: 45
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:
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
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
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
Reputation: 23015
If you have a number like 1234
and 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