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).
Related
int a=000123,b=1
int len=(int)Math.log10(a)+1;
while((a=a/10)!=0)
{
++b;
}
System.out.println("Number of Digits:"+b);
output:
Number of Digits:3
it does not count the zero's if it takes place at left side. is there any way to count the digits with zero's also.
If its an int (and not a String), it isn't stored how many zeros are in the front when you defined it -- 000123 is exactly the same as 0123. However, starting a number with zero (one or several) means you are defining a number in octal representation. Thus int a=000123; System.out.println(a); will output 83, probably not want you mean.
Hi I don't think you can do that
best thing to do is get the number as a string and cast it to an int using nteger.parseInt when you want to calculate things
I am curious if it's even possible due to Ascii characters only having 3 digit integer codes. A random number between 3 and 7 digits just sounds outrageously large. Please let me know if I'm thinking about this all wrong.
You must first take in the person’s first name.
Then, you must take in the person’s last name.
Then, you must convert the first and last letter of the person’s first name and last name into their ASCII values.
Then, you must shift their ASCII value over by a random number between 3 and 7 digits.
If your number goes past z, then, you must wrap around to a. You cannot use if statements
The requirements don't say that the shifted value will fall into the ASCII range. Actually, the behaviour for the overflow is explicitly stated in the last point.
I think your teacher is trying to make you use the modulus operator. I won't give you a full solution but you should look into this direction.
I'm trying to write a method in java that will take an input of any number of 0 or 1 digits and output that line after being encoded with Hamming Code.
I have managed to write the code when knowing the number of digits the input will have (in this case 16) because knowing the number of digits in the input, I immediately know the number of parity bits there have to be added (5 in this case) to a total of 21 digits in the final output. I am working with int arrays so I need to declare a size in the beginning and my code works based on those exact sizes.
Can you guys think of any way/algorithm that can give me the number of digits the output will have (after adding the relevant parity digits to the number of input digits) based solely on the number of input digits?
Or do I have to tackle this problem in a totally different way? Any suggestions? Thank you in advance!
Cheers!
From my understanding, you get your 6th parity bit at 32 bits of input, 7th at 64, etc. so what you need is floor(lg(n)) + 1, which in java you can get by using 32 - Integer.numberOfLeadingZeros(n).
Assuming your input is made up entirely of 0s and 1s, you would do
int parityDigits = 32 - Integer.numberOfLeadingZeros(input.length());
Is your input a String or individual bits? If you input as a String, you can convert each character to a bit, and the length of the String gives you the length of the array.
If you need to input the bits one at a time, store them in an ArrayList. When all bits have been entered, you can convert your list to an array easily, or use the size of the list etc.
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.
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().