Reputation: 41
So I'm fairly new to java and to programming in general, I hope someone could help me with this. Also I'm aware that there are unnecessary syntax in the code but that's because it's not exactly what I have to do for my assignment but I modified it so that it will run on the main instead of inside another class.
I've been stuck on this problem for a while and I can't seem to figure out how to print out the array. I THINK that my formula to get the binary is correct, but whenever I would try to print out the array it would give me : [I@5df86e79
int value = 69;
int length =8;
int remainder1 = 0;
int[] bits = new int[length];
int bitstring=0;
while(value>0 && length<32 && length>0){
for(int i=bits.length;i>0;i--){
remainder1=value%2;
value=value/2;
bits[i-1] = bitstring+remainder1;
}
}
System.out.println(bits);
Upvotes: 0
Views: 121
Reputation: 513
You can't do it like that you have to convert to string
Arrays.toString(bits)
Upvotes: 0
Reputation: 106
An array in Java is an Object. Unlike primitives (int, char, etc) you must specify how it should be represented when you call System.out.println().
User defined objects need to override the toString() method which will be called.
In this case though, since Arrays are already defined in Java, you can simply call the class's toString method on an array of integers.
Upvotes: 1