Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Storing variables in an array

Tarrell Fletcher
Ranch Hand
Posts: 60
I am trying to create code that converts a number to binary. I have the main code done I think..but I want to store the elements in an array and then possibly reverse the order to show the binary form.

I set "n = 1" and then label the array with "n" elements so it can get all the numbers after the final iteration. I just need to know how to store "r" in that array, and also could I possibly reverse the array by putting [ n-1] as the output?

Tarrell Fletcher
Ranch Hand
Posts: 60

I put this inside the while loop but I get an error that says "Index is out of bounds" when I try to output the data.

Greg Charles
Sheriff
Posts: 2993
12
I think what you're trying to do is convert an integer to a binary string. For example, for the input 11, the output would be "1011". Is that right? The reason you might need to reverse an array is that it's easier to figure out the bits from right to left.

The calculations you're doing look OK to me, but you're just printing your results to the screen. What you want to do is start building up a String with the r's as you calculate them. The last thing you'd do then is reverse the String.

This creates an array with n elements with indexes from 0 to n-1. The means a[n] is definitely going to be a problem, because that's one past the last element. Now, "r" is what you put in the array, so using it as an index doesn't make sense either. That is, binary[r] isn't what you want to be doing.

Tarrell Fletcher
Ranch Hand
Posts: 60
Oh ok I see what you are doing. I have to save the "r" which is my remainder as a String and then I can store them in the array through iterations? I saw the Integer class thing when I did a google search, but we aren't allowed to use that class. Our instructor actually wants us to create the code. I have to do conversions from 2, 8, 10 and 16 and back. I think thats like 32 different methods right lol. But someone told me that its easier to turn any other base that isn't binary to binary and then from binary to the base. So I am just trying to code this first and then go about it.

Tarrell Fletcher
Ranch Hand
Posts: 60
Also I have system.out.print statements all over because I was having a problem creating the loop. So I just did the statements to see if the iterations were running correctly.

Greg Charles
Sheriff
Posts: 2993
12
No, you surely don't want 32 methods, but the loop you have (I think) does the job for you.

11 % 2 = 1 (1 is the first thing you've computed)
11 / 2 = 5 (Integer division truncates away the remainder. This is next input for your calculations)
5 % 2 = 1 (1 is the next computation)
5 / 2 = 2 (This is the next input)
2 % 2 = 0 (0 is the next computation)
2 / 2 = 1 (1 is the next input)
1 % 2 = 1 (1 is the next calculation)
1 / 2 = 0 (done)

OK, so all your computed values are 1,1,0,1. You need to reverse that to get "1101". That's the right answer for the binary of 11. The cool thing is that the same process will work on any positive integer you give it.

Tarrell Fletcher
Ranch Hand
Posts: 60
Yea I just revised my program it will display the remainders, but I have to figure out how to store them so I can reverse the order to get the binary form. Here's my new loop statement that works perfectly.

I tried it with different numbers and I get I get the answer in binary form but it isn't exactly binary until I reverse the order.

Campbell Ritchie
Sheriff
Posts: 50687
83
The reason is that when you do the i % 2 bit, you are working out whether the number is even or odd, or whether the last digit is 0 or 1. Then you divide by 2, and repeat the process to find whether the quotient was even or odd, which gives the value of the penultimate bit. Etc., etc.

What you want to do is to create an array big enough to hold all the bits (remember an int occupies 4 bytes = 32 bits of memory), and fill it in from right to left.Since an int[] is filled with 0s as a default, you will get {0, 0, ..., 1, 0, 1, 1} from 11.

Harsha Smith
Ranch Hand
Posts: 287
Don't re invent the wheel
one line code

Campbell Ritchie
Sheriff
Posts: 50687
83
• 4
The whole idea of these exercises is to reinvent the wheel.

Winston Gutkowski
Bartender
Posts: 10527
64
Tarrell Fletcher wrote:so I can reverse the order to get the binary form...

Think like a programmer. If you don't have it (or know where to find it), write it:
Winston

Tarrell Fletcher
Ranch Hand
Posts: 60
Yea I have to mess around more with arrays because they a bit ehh to me right now. But I found some way to reverse it but it wouldn't work.

For some reason this didn't work.

Campbell Ritchie
Sheriff
Posts: 50687
83
You cannot create a StringBuilder with a char in its constructor. Go through the different StringBuilder constructors and work out what the JVM will interpret that as. And no, it will not convert it to a String.