Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Two-dimensional boolean array?

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Two-dimensional boolean array?

    Hi,
    I'a beginner in Java language. I saw the following exercise from the following web:
    http://introcs.cs.princeton.edu/14array/
    I tryied to solve it, but can't get it. Please help .

    Write a code fragment that prints the contents of a two-dimensional boolean array, using * to represent true and a space to represent false. Include row and column numbers.

    below is my effort....
    public void 2Ddisplay() {
        	for (int x = 0; x < 2Darray[0].length; x++) {
        	for (int y = 0; y < 2Darray.length; y++) {
        	if (pixels[x][y] == true) {
        	System.out.println("*");
        	} else {
        	System.out.println(" ");
        	}
     
     
        	}
        	}
        }
    boolean 2Darray[][];
    int w,h;
    Thank you
    Last edited by tcmei; May 17th, 2011 at 07:02 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Two-dimensional boolean array?

    The instructions say to use * to represent true and space for false. Therefore you need a 2D char array not a boolean array.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Two-dimensional boolean array?

    There's only a 2 possible values, therefore a 2d boolean array will work perfectly fine (not to mention that the problem specifically says to represent the contents of a 2d boolean array as such ).

    You say you can't get it, what did you mean by that statement? Are your outputs wrong? Or are you getting an exception/compile error? If so, please post the message.

    One thing I can see right away is that you're printing out each element on it's own line. You might want to consider using System.out.print to print out a row, then use an empty System.out.println to advance to the next row.

Similar Threads

  1. Two-Dimensional Array to Grade Students?
    By Java Neil in forum What's Wrong With My Code?
    Replies: 37
    Last Post: March 22nd, 2011, 03:47 PM
  2. Single Dimensional Array Help!
    By Allicat in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 15th, 2011, 12:01 PM
  3. Two-Dimensional Array and Loops
    By astrojunk in forum Java Theory & Questions
    Replies: 2
    Last Post: February 11th, 2011, 07:18 AM
  4. Finding the length of a two dimensional array
    By petemyster in forum Java Theory & Questions
    Replies: 2
    Last Post: December 12th, 2010, 10:21 PM
  5. 2 dimensional array alternative ???
    By zeeshanmirza in forum Java SE APIs
    Replies: 1
    Last Post: February 23rd, 2010, 06:18 PM