Results 1 to 3 of 3
  1. #1
    blackz is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default complete the code

    Java Code:
    for (int i = 0; i <= 1 ; i++)
    for (int j = 0; j <= 1; j++)
    for (int k = 0; k <= 1; k++)
    System.out.println(""+i+j+k);
    The above code prints out:
    110
    111

    Complete the following code so that it produces the same output as the above
    code.
    Java Code:
    for (int i = 0; i < 8; i++)
    System.out.println(" "+ );
    I cannot get it how can I reduce 8 lines to 2 to make same as above.

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: complete the code

    Better to show your code with good indentation style so that it's readable:

    Java Code:
          for (int i = 0; i <= 1; i++)
             for (int j = 0; j <= 1; j++)
                for (int k = 0; k <= 1; k++)
                   System.out.println("" + i + j + k);
    And this will print out a bit more than you show. In fact it prints out:

    Java Code:
    000
    001
    010
    011
    100
    101
    110
    111
    which is the binary representation of the ints from 0 to 7.

    So what you need is some way to convert a number to its binary equivalent and then print it with a char width of 3, and with padding 0's on the left if the number is < 3 char in width. The first place I looked was at the Formatter#format method which is most often used as System.out.printf, but that doesn't do binary, only decimal, octal and hex. So I next created this hack:

    Java Code:
          for (int i = 0; i < 8; i++) {
             String binaryString = Integer.toBinaryString(i);
             int decimalHack = Integer.parseInt(binaryString);
             System.out.printf("%03d%n", decimalHack);
          }
    which works, but it's cheating.

    But seriously as to your problem, you need to figure out how to convert your number to its binary representation, and then simply pad those 0's to the left of the String if the String length is < 3. You should be able to figure this out on your own.

    Luck!

  3. #3
    blackz is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: complete the code

    My mistake, I made an error in the code formatting thats why it was showing 110 and 111 in the console. Now I am working on it.

    Thanks

Similar Threads

  1. complete sequence
    By aizen92 in forum New To Java
    Replies: 25
    Last Post: 12-29-2010, 12:58 PM
  2. can anyone help me,a complete newbie?
    By keye906 in forum New To Java
    Replies: 13
    Last Post: 06-02-2010, 02:56 PM
  3. ejb 3.0 complete tutorial
    By better_me007 in forum Enterprise JavaBeans (EJB)
    Replies: 3
    Last Post: 09-28-2009, 11:52 AM
  4. Help me to complete this class, please
    By pink_872 in forum New To Java
    Replies: 6
    Last Post: 04-21-2009, 02:23 PM
  5. Complete code won't run in eclipse
    By confusedStudent in forum New To Java
    Replies: 2
    Last Post: 12-19-2008, 09:43 AM

Tags for this Thread

Posting Permissions

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