Results 1 to 4 of 4
- 03-07-2011, 09:36 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
How to: Select specific array indices?
Hello! I'm new to these forums, Java, and programming in general, and I could use some help.
For an assignment I need to write a program that converts a decimal number into the equivalent binary, octal, and hexadecimal numbers. I can convert them just fine, but the assignment requires that I format the binary output with a dash between each set of 4 bits (like so: 1011-1101-0001-0101), which I'm having trouble with.
What I'm trying to do, below, is convert the decimal number into an array of binary numbers (which is working), and use a for-each loop to print the array and a selector to insert the dashes (which is not working):
Java Code:public class tester { public static void main(String[] args) { int decimal = 1000; int[] bin = new int[16]; convert(decimal, 2, bin); System.out.println("Dec: " + decimal); System.out.print("Bin: "); for (int u: bin) { if (bin[u] % 4 == 0) { System.out.print(u + "-"); } else { System.out.print(u); } } } public static void convert(int dec, int base, int[] array) { for (int i = (array.length - 1); i >= 0; i--) { array[i] = dec % base; dec /= base; } } }
What I was hoping to receive was:
Java Code:Dec: 1000 Bin: 0000-0011-1110-1000
And what I got was:
Java Code:Dec: 1000 Bin: 0-0-0-0-0-0-111110-10-0-0-
I see what the problem is: Java is testing the element instead of the index itself. Is it possible to check the index itself?
Thank you in advance.
::Edit:: I think, now, that I have figured a way to do this using a regular for loop, but I'm still curious if/how you can select specific indexes.
::Edit Two:: For anyone who might be interested, here is a for loop in which I get the proper output:
Java Code:for (int i = 0; i < bin.length; i++) { if ((i + 1) % 4 == 0 && i != 15) { System.out.print(bin[i] + "-"); } else { System.out.print(bin[i]); } }Last edited by louist; 03-07-2011 at 09:57 PM.
- 03-07-2011, 10:04 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
The problem is the for each loop, which you figured out, it just continues as long as the list has more items. You could use indexOf but since you have all ones and zeroes you would get a wrong answer. It looks like for this solution the for:each loop is a bad approach.
- 03-07-2011, 10:18 PM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thank you for the response.
Are there ever cases where you might do something more complex with a for-each loop than just run through every element?
- 03-07-2011, 10:21 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Using GUI to select a shape object from and array
By Davededo in forum New To JavaReplies: 3Last Post: 10-17-2010, 07:03 AM -
DnD - JList indices
By carderne in forum New To JavaReplies: 0Last Post: 03-09-2009, 06:03 AM -
How to retain value in struts 2 using <s:select></s:select> tag
By SaiPrasad@Sella in forum Web FrameworksReplies: 0Last Post: 02-09-2009, 07:23 AM -
how do i print a specific txt file on a specific printer
By nikhilbhat in forum New To JavaReplies: 2Last Post: 11-08-2008, 10:40 AM -
Select specific cell
By Echilon in forum New To JavaReplies: 1Last Post: 01-01-2008, 07:47 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks