need help with parseInt - java

how can i get for example the integer codeInt=082 from String code='A082'
i have tried this:
int codeInt = Integer.parseInt(code.substring(1,4));
and i get codeInt=82 ,it leaves the first 0 but i want the full code '082'.
i thought of parseInt(String s, int radix) but i don't know how .
any help will be appreciated .
thanks.

An integer just stores a number. The numbers 82 and 082 (and 0082 and 000000082 for that matter) are exactly the same (unless you put them into source code in some languages in that manner, then you'll get a compiler error)1.
If you desperately need the leading zero, then you should either leave it as a string, or format the number appropriately for output later.
1 Due to the C designers having the ingenious idea that writing octal constants with a preceding zero would be cool. As if something like 0o123 would have been that hard to implement once you already got 0xf00 ...

The number 82 and 082 and 0082 is mathematically the same number, and is represented by the same sequence of bits. You can't encode the number of leading zeroes in an int (although you can certainly print it with whatever format you choose).
Note also that the number 082 is different from the Java literal 082, which is an (invalid) octal literal.
int i = 010;
System.out.println(i); // this prints 8

082 is not an integer. It's a string representing the integer 82. If you require leading zeros to be left untouched, you will need to work with strings. If you only need it to print 082, you can use java.text.MessageFormat or System.out.format() or other, similar solutions to print it that way.

If you want 0000123 then you need to threat a variable as a String instead of Integer. Simply: 123 is equal to 000123 and 0123 and 0000...1 billion zeros here...000123.
But if you just want to display a number with fixed length then use System.out.format().

Related

How to add leadings Zeros to a BigInteger?

I want to add leading Zeros to an BigInt, if the BigInt has less than 10 Digits it should add padding Zeros and I have to calculate with the number later on.
Thats my solution so far:
BigInteger bankAccountNumber = new BigInteger("999999999");
BigInteger zero = new BigInteger("10000000");
public void checksum(){
if(bankAccountNumber.toString().length()<10){
while(bankAccountNumber.toString().length()<10){
}
System.out.println(bankAccountNumber);
}
System.out.println(bankAccountNumber.toString().length());
}
My problem is I cant come up with a methode that includes that bankAccountNumber is a real Number with leading 0`s because I have to combine the Number later on with anouther BigInt and this should include leading zeros, all I got is an outprint with leading Zeros, but I cant caculate with that,so thats my problem, thank you for your help.
CAVEAT: As some comments have already pointed out, you're fundamentally applying the wrong approach here. If 'add 1' doesn't make sense to a thing, then it is a not a number. Adding 1 to a bank account doesn't do anything sensible, hence, you should be storing that as a String most likely. Anything but a class that extends Number.
BigInteger doesn't work like that. It's not a light wrapper around a string. You cannot add leading zeroes to it.
Instead, when printing the BigInteger (turning it from its standard memory representation to a bunch of characters, e.g. to show to a user), you indicate how you want to do that, and as part of that, you can say: ... pad with zeroes if need be.
BigInteger example = BigInteger.ONE;
String ex = String.format("This is the number: %012d", example);
System.out.println(ex);
This prints This is the number: 000000000001 as you wanted. %012d is string-format-ese for: "I want to print an integer number (d), take up at least 12 character slots (12), and if 12 is larger than the number of chars needed to render example, pad it out with zeroes (0).

How to parse String into Integer upto two digits in java

I have a String which contains values like 12:02.Now i splitted this String based on :
and stored into array.Now i have to parse these array values into integer ..
On parsing to Integer from String my 02 becomes 2 only whereas in need 02.
I am not getting how to do it.
Here is my code..
time = request.getParameter("time");
System.out.println(time);
String[] timearr = time.split(":");
hourset=Integer.parseInt(timearr[0]);
minuteset=Integer.parseInt(timearr[1]);
the value of minutset is giving difference..
Please help me.
Thanks in advance..
You can use this one
If you want to see "02" on screen, format it with "%02d" pattern,
System.out.println("%02d", data)
You are confusing the value of a number (2) with its string representation (which can be 2, 02, 002, 000002, 0x02, whatever).
If you're dealing with time, use one of the classes that are more suitable for that, e.g. Date and Calendar.
If you really want to keep the '02', use a String and not an int.
You have to distinguish two things: how number looks like and what number is. For example number 10 may look like "010", "10", "A", "1e1", but it is still the same number.
When you output number on screen with System.out.printf("%d", minuteset), you are asking to format it with simplest format - "2". If you want to see "02" on screen, format it with "%02d" pattern:
System.out.printf("%02d", minuteset)
it's not possible. 02 is becomes 2 only..
02 is not an Integer. 2 is an Integer. and you can not use Integer in this case. but if you want to store it in Integer, you must parse (you must write a custom parser that insert 0 to the first of one digit numbers) it again and show it as String.
That is because an actual number has no leading zeroes as they make no sense at all and are superfluous. In case of integers, if you specify a leading zero yourself, then it'd be treated as an octal value.
In case you want to display the number with a leading zero, then you can use string formatting to achieve this.
int num = 123;
string leadingZeroes = String.format("%05d", num);
Numeric data types are fundamentally incapable of distinguishing between 2 and 02. The underlying bit pattern is identical. If you try System.out.println(02); you'll see 2 as the output, because it just can't tell the difference.
Whether or not to put zeros in front of the number is a formatting issue, and you can only control formatting of a number once you output it as a String again. E.g. try:
String time2 = String.format("%02d:%02d", hourset, minuteset);
And you will get "12:02" again.
Golden rule: If you have numbers and you know that you will not do any math operations or database operations on the numbers, you should keeps those numbers as strings. I don't know if this is a real golden rule, but this is what I follow. lol.
Strings have many power ways to be manipulated, that's why I prefer String if no math/db operations will be done.

String.format() and hex numbers in Java

I'm trying to figure out why String.format() is behaving the way it does.
Context: Systems programming class, writing an assembler.
There is a 5 character hex field in the object file, which I am creating from a value.
Tried using: String.format("%05X", decInt);
This works as intended for positive numbers
(11 -> 0000B)
However it fails for negative numbers
(-1 -> FFFFFFFF instead of FFFFF)
I suppose I could just take a substring of the last 5 characters, but I would still like to figure out why it behaves this way.
The width used in format is always a minimum width. In this case, instead of using sub string operations I would suggest:
String.format("%05X", decInt & 0xFFFFF);
Format width only works to create a minimum number of digits, and only has effect on leading zeroes.
Instead of substring, you could use a bit mask:
String.format("%05X", decInt & 0x0FFFFF)
By the way, 11 -> 0000B, not 0000A as claimed in your question.

What kind of number is aa568 and how do I convert to it from decimal in Java?

I assume aa568 uses a different base than 10.
What type of number is this most likely?
And how do you convert a decimal number into this base using Java?
Could it be hexadecimal? If it is then just precede that by 0x ie. 0xaa568.
Assuming it is hexadecimal (0-9 + A-F instead of 0-9), you can convert it from hex to decimal as follows:
int i = Integer.parseInt(hexStr,16);
Where 16 is the base of the number system. Decimal is base 10, hexadecimal is base 16.
And back from decimal to hexadecimal:
String hexStr = Integer.toHexString(i);
Converting a number into a hexadecimal string can be done by the Integer.toHexString() method:
int number = 697704;
System.out.println(Integer.toHexString(number));
will print "aa568".
I'd assume that it's hexadecimal and more typically written 0xAA568 which is decimal 697704.
If you're asking how you would print a decimal number in a hexadecimal representation using Java ... see this stackoverflow article.
What kind of number may depend on the context, of none of the above fits, then it may be an integer sequence, I looked it up in the online encyclopedia of known integer sequences. Example a000045 is a Fibonacci sequence, (Formerly M0692 N0256), if it has to do with math it may also be a library reference to a paper written by AA. Only the Context can tell. Mathnet.ru had one reference "The behavior of the Lebesgue constants of two-dimensional Fourier sums over polygons" which fits the questioned number aa568.

Padding a number with zeroes

Lets say I have a number 345, I want to have so that I end up with 0345. I.e.
int i = 0345;
How can I take an existing number and shift it along or append a 0.
I know you are talking about an int, but maybe what you want is to pad a number with leading 0s. A quick way is with the String.format static method.
int num = 345;
String.format("%04d", num);
would return:
"0345"
The 4d tells it to add 0s to the left if it has less than 4 digits, so you can change it to a 5 and it would give you:
"00345"
Using a 0 on the start of the number when declaring it means it's octal, so 0345 is actually 229 in decimal. I'm not sure how you expect to add a zero to a number using bitwise operations, which work on the binary representation of the number. If you want to add it to the decimal representation, it won't mean anything, since the number is always stored in binary, and the value is converted for your convenience to decimal when being displayed. When doing any computations, the decimal value is not important, only the binary one.
If you're interested only in displaying the value with a 0 at the start, then you could append the 0 to a String containing that number which can be easily done like this "0" + i.

Categories