Results 1 to 4 of 4
  1. #1
    louist is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default 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 10:57 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    louist is offline Member
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Default

    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?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I am not experienced enough to give you a truly solid answer. Try in your original for each loop adding a print line to print the indexOf the item and see what it prints. It may allow you to determine where the item is stored

Similar Threads

  1. Using GUI to select a shape object from and array
    By Davededo in forum New To Java
    Replies: 3
    Last Post: 10-17-2010, 08:03 AM
  2. DnD - JList indices
    By carderne in forum New To Java
    Replies: 0
    Last Post: 03-09-2009, 07:03 AM
  3. How to retain value in struts 2 using <s:select></s:select> tag
    By SaiPrasad@Sella in forum Web Frameworks
    Replies: 0
    Last Post: 02-09-2009, 08:23 AM
  4. Replies: 2
    Last Post: 11-08-2008, 11:40 AM
  5. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 08:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •