Using String.format to remove particular digits from an integer - java

Say we have a number
int number = 1234
and I want to use formatting to extract the final two digits as a string 34. How can one accomplish that?
I attempted:
String extraction = String.format("%d", number)
but this simply copies the entire number. I'm new to the syntax used by the formatter and can't seem to figure out the syntax that can go inside the characters of a number (or a String, for that matter) and pull them out. I've found a solution using charAt methods but am particularly curious about whether it's possible to accomplish it using formatting techniques.

Going via a String is inefficient and unnecessary to extract the last two digits of an integer.
Simply:
int lastTwoDigits = number % 100;
If you do want to go via a String, you can use:
String s = Integer.toString(number);
s = s.substring(s.length() - 2);
int lastTwoDigits = Integer.parseInt(s);
(note that this handles -ve numbers slightly differently to the first suggestion).

Related

Converting Long Object to String in Java

Let's say i have this Long in Java:
Long x=0b01101L;
and i want to convert it into a String using this:
String str1 = Long.toBinaryString(x);
Is there a way to keep the first 0 inside the String?
What i mean is can this:
System.out.println(str1);
print 01101 and not 1101?
This is not possible. When you declare Long x = 0b01101L;, you create a long instance, which contains no information about the String representation of the value it contains. Yes, it equals 13 in decimal, but it also equals 十三 in traditional Chinese/Japanese writing, and it also equals 10 + 3, and so on.
If you want to convert it to a String padded to a certain number of zeroes, you can use String.format("%16s", Integer.toBinaryString(x)).replace(' ', '0'), but you must know in advance how many digits you want to be printed, as this will always return 16 digits.
However, this information is not encoded in a Long (or long). If you need to keep this information, declare your variable as a String xString = "01101";, and convert it to a long using Long.valueOf(xString, 2); when you need to do numerical operations.

How to convert string to number in java ? But i am not looking for hashCode as it wont give unique number. at least i want math logic for same

Hi I want to convert string to some unique number in java.
Exmple: "Production-0-1" to 100021
"Process-23-30" to 12310
And all return number has to be unique.
I dont wanted to use hashCode as they can return duplicate like "Aa" and "BB" has same has code.
Let me know math logic to create this is no method available.
String random = "Production-0-1";
String bi = new BigInteger(random.getBytes("UTF-8")).toString();
BigInteger numBig = new BigInteger(bi);
System.out.println(numBig);
Based on #markspace comments, I tried the following and every time it produces random unique number but beware if you have a very large String and a limited memory space then the output may go out of bound.

What datatype to use for 40 digit integers in java

For example
first number = 123456.....40 digits
second number = 123456.....40digits
Then I need to store the number
third = first * second;
after that I need to print third and again I need to perform operation like
fourth = third * third;
and print fourth. So how can I handle that much long integers which data type I need to use?
Use BigInteger class in java.math, then use BigInteger.multiply to multiply them together.
Check here for more on how to use it:
https://www.tutorialspoint.com/java/math/biginteger_multiply.htm
See this question its similar Arbitrary-precision arithmetic Explanation
the answer explain it quite good.
The basic is that you work with smaller parts. Just remember how you learned to work with big numbers in school (2-3 grade) you wrote down two numbers and
2351
*12
-----
4702
2351
------
28212
You just do small operations and store them somewhere you can put them in string or better in some array of integers. Where for example
number 123456789123456789 can be
number[0] = 6789
number[1] = 2345
number[3] = 7891
number[4] = 2345
number[5] = 1
String numberToShow = "";
for(int i = 0; i
There are some links for computer arighmetics
https://en.wikipedia.org/wiki/Category:Computer_arithmetic
and for adders
https://en.wikipedia.org/wiki/Category:Adders_(electronics)
In your computer you have basically also just some adders which can work only with some size of numbers and if you need to work with bigger you need to split it in smaller parts.
Some of this parts can be done parallel, so you can speed up your algorithm. These are usually more sophisticated.
But the basic principe is similar to working with big numbers on your primary school.

Getting each different numbers separately and examining them in java

I am new to java and i want to make a program where i want to get the integers the user has input, and look at what different numbers they are. and move them around as well as add stuff between them.
For example:
Enter number
User input: 123456
after that i want to see what the first number is, which is 1 or what the third number is which is 3. i want to be able to get those different information. and play around with them, for example add the sixth and the first number and print it out for the user.
Which technique in java allows me to do that, what is the name so i could study it more.
if there is a video online teaching that, it would also be great.
Thanks.
ps. i know the basics, i dont need to know how to do the second part playing with the numbers i just dont know how to tell what the first or second number in the lists of number is.
Eg: 123456
Convert the int to string keep the int also
1) String.valueOf(number)
2) Integer.toString(number)
get the length of the string using String.length();
the if you want the 3rd number
Use the int value for the following operation
Eg: 123456 (3rd number 3) Since you have the length of the string you know what will be the divisor
divide using 10 * 10 * 10 this will 123456/1000 = 123
Then % it with 10 always you can put these within a methos to get any position
Try the below code :You are taking an integer input and converting it into a string
public class InputNumberProgram {
public static void main(String[] args) {
Integer i = 5678;
// Convert your input to a string
String s = Integer.toString(i);
System.out.println("Length of input:" + s.length()); System.out.println(Integer.parseInt(s.valueOf(s.charAt(0)))+Integer.parseInt(s.valueOf(s.charAt(1))));
}
}
If you read it as a String and do what you want with String methods
For example you have String number. To get i-th symbol you can use number.charAt(i)

How can you compare strings in android with greater than

I was wondering if there is a way to compare strings in android with greater than or >.
Lets say I have this:
String numbers = number.getText().toString();
if (numbers.equals("9")){
output.setText("50");}
so if you enter 9 in the number EditText field the output TextView will display 50.
I have quite a few different numbers that will then = a different number but what can I do if I want 10,11,12,13,etc to = 100?
Is there a way to do this by using something like this?
if (numbers.equals("9"++))
or is there some kind of wildcard in android like
if (numbers.equals("1"+"*"))
i know if i replace the * with zero it will be 10 so if this is possible I could make one for 1, one for 2, one for 3, etc. and this would still save me so much code.
If this is not possible let me know if you have any ideas.
Thanks guys!
You'll need to convert the String to a number first. Something like this:
int number = Integer.parseInt(number.getText().toString());
if (number > 9)
{
output.setText("50");
}
If the String is not guaranteed to be a valid integer you'll have to handle NumberFormatException too.
Is there a reason you can't use
Integer.valueOf("9");

Categories

Resources