check logic 0 in reverse number program in java [duplicate] - java

This question already has answers here:
Java reverse an int value without using array
(33 answers)
Closed 4 years ago.
This is the logic to reverse a number. But this logic doesn't reverse the number having 0. for example i want to reverse 70 means it will give output as 7.
so kindly give logic to reverse number having 0. Thanks.
while(num>0)
{
rem=num%10;
sum=(sum*10)+rem;
num=num/10;
}
System.out.println("Output is:"+sum );

Integer numbers cannot have leading zeroes.
What would be the use case in which such a function be useful ?
If the purpose is just to reverse an integer number and use it as a string,
then this should do the job.
int num = 70 ;
String numStr = new Integer(num).toString();
StringBuilder numStrB = new StringBuilder(numStr);
String reversedStr = numStrB.reverse().toString();

Related

How can I pad a hexadecimal digit with 0 in java? [duplicate]

This question already has answers here:
Integer to two digits hex in Java
(7 answers)
Closed 1 year ago.
I'm trying to pad a hexadecimal digit with 0's in the beginning so that the length of it is always 4. For example, the FF is going to be padded as 00FF. I have tried to use String.format("%04d", number) but it didn't work as my hexadecimal digit is actually a string.
I have also tried using StringUtils.leftPad() but for some reason, IntelliJ couldn't detect it.
Please note my count is going to change, but I haven't represented it here as I'm yet to implement it. This is my sample code. Thanks.
public static void main(String[] args) {
int count = 0;
String orderID = Integer.toHexString(count).toUpperCase();
System.out.println(String.format("%04d", Integer.valueOf(orderID)));
}
String.format("%04x", 255)
with output
00ff
(or use X for uppercases hex digits)
As #user16320675 point, more info about Formatter here.

How to convert from int to int array in Java? [duplicate]

This question already has answers here:
Convert an integer to an array of digits
(24 answers)
Closed 2 years ago.
I would just like to ask how to convert an int to int array - for example:
int number = 12345;
to:
[1,2,3,4,5];
Btw - could not really find anything out there, so we may hope that someone know.
thanks in advance
You can do it like this:
int number = 12345;
int[] digits = String.valueOf(number).chars().map(c -> c-'0').toArray();
Explanation:
First convert int to string with String.valueOf(number) to be able to use the method chars() which get a stream of int that represents the ASCII value of each char in the string. Then, we use the map function map(c -> c-'0') to convert the ASCII value of each character to its value, subtracting the value of the ASCII code from the character '0' from the ASCII code of the actual character. Finally, the stream is converted to an array with toArray().

Extract the value from number after dot in java [duplicate]

This question already has answers here:
How do I get the part after the decimal point in Java?
(15 answers)
Closed 6 years ago.
int number = 24.24;
int afterDot = (int) (number*100)%100;
afterDot = 24
This logic is wrong if
int number1 = 24.4
How can I get the 4 from 24.4 ?
Whatever will be the number but want to extract the value after dot.
Actually the formula has to work for both type of value.
If you convert your double to a string, the problem becomes easier :
double number = 24.4;
String numberAsString = String.valueOf(number);
String decimalPart = numberAsString.split("\\.")[1];
System.out.println(decimalPart);
int number1 = Integer.valueOf(decimalPart); // NOTE: This conversion is lossy.
System.out.println(number1);
Note that by converting your decimalPart string (e.g. "001") to an integer (1), you might lose some information.
With 24.4, it outputs :
4
4
With 24.001, it outputs :
001
1
With 3d, it outputs:
0
0

Fixing Disappearing Zeros [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 6 years ago.
I wanted to divide the integer and store it in an array
For Ex:1000000000000 into two indexes
arr[0]=1000000
arr[1]=000000
but arr[1] stores it as 0 instead of 0000000.
I wanted to perform some operations with it,so i needed 7 zeros in it ,instead of 1 zero.
Is it achievable in some way ?
Convert your number to a string then use substring() to split it up.
long num = 1000000000000L;
String str = num + "";
String[] array = new String[2];
array[0] = str.substring(0, 6);
array[1] = str.substring(7);

How do I have a random int from 1 to 6 using math.random() in java? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 8 years ago.
I'm writing a method in java which relates to a die throw and I try to use the math.random() but i realized that 0 is included in the random integers involved. Additionally, I don't quite get the *7 part what does it mean?
I went to research from the Java API but it doesn't mention any bit about this or is it I am doing the wrong research? Thanks so much for reading!
public int dieThrow()
{
int num = (int)(Math.random() *7); //returns an integer
return num;
}
This is a pretty simple exercise. You observe that 0 is a possible outcome, so you simply + 1 to the result, like so:
public int throwDie()
{
return (int)(Math.random() * 6) + 1;
}

Categories

Resources