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 2 of 2

Thread: 2 dimentional boolean array problems!

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy two dimensional boolean array problems!

    Hi


    How can I do a matrix code with a boolean expression and then output it?

    I know that when you declare a 2 dimensional boolean array that all the elements are already false. So how can I display them elements that are false with a '.' and if true with a 'O'?

    Sorry for the dumb amateur question but I just cant do it!

    Thank you for any help given!

    I have tried this:

     
    	{
    		       for(int i=0;i<matrix.length;i++)
    		       {
    		          for(int j=0;j<matrix[i].length;j++)
    		          {
    		             if(matrix[i][j])
    		             {
    		               System.out.print("O");
    		             }
    		             else
    		             {
    		               System.out.print(".");
    		             }
    		          }
    		            System.out.println("");

    How ever the problem here is that I get a nullPointerException error appear.

    The full code is:


    /**
     * Class LEDDisplay - write a description of the class here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class LEDDisplay
    {
    	/* private class constants */
    	private final int FONT_LETTER_HEIGHT = 5;
    	private final int FONT_LETTER_WIDTH = 6;
    	private final int LETTERS_PER_DISPLAY = 10;
     
     
    	/* instance variables */
    	private boolean [][] matrix;
     
    	/**
    	 * Constructor for objects of class LEDDisplay
    	 */
    	public LEDDisplay()
    	{
    		boolean[][] matrix = new boolean [FONT_LETTER_HEIGHT] [FONT_LETTER_WIDTH * LETTERS_PER_DISPLAY];
    	}
     
    	/*
    	 * This display() method will print a textual representation of matrix[][] onto the display pane.
    	 */
     
    	public void display()
     
    	{
    		       for(int i=0;i<matrix.length;i++)
    		       {
    		          for(int j=0;j<matrix[i].length;j++)
    		          {
    		             if(matrix[i][j])
    		             {
    		               System.out.print("O");
    		             }
    		             else
    		             {
    		               System.out.print(".");
    		             }
    		          }
    		            System.out.println("");
     
    }
    	}
    }
    I think that I need to add something to do with a 'char' >? But I am a complete amateur and have no idea where to sort this out!

    I just want to to output a display like this:
    .................................................. .................................................. ......................................
    .................................................. .................................................. ......................................
    .................................................. .................................................. ......................................
    .................................................. .................................................. ......................................
    .................................................. .................................................. ......................................

    at this stage as each '.' represents a false value in that 2 dim array.

    I also think that its something to do with the fact that I have declared matrix twice? Again I still have no idea how I can sort it!?

    The error I get at this stage is:
    Exception: line 2. java.lang.NullPointerException

    Which is crazy as line 2 is fine!

    I have been going crazy for weeks on this and what annoys me is that its such a simple thing! Please help me before I go crazy !

    Again thank you for any help you give on this!

    Regards
    Last edited by masterT; July 14th, 2011 at 10:55 PM. Reason: spelt dimensional wrong! sorry about that!


  2. #2
    Junior Member
    Join Date
    Jul 2011
    Posts
    8
    My Mood
    Depressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: 2 dimentional boolean array problems!

    Ok I have resolved this now! Thanks for looking.

Similar Threads

  1. Implementing a two-dimensional boolean array
    By eNxy in forum What's Wrong With My Code?
    Replies: 40
    Last Post: July 16th, 2011, 06:30 AM
  2. Problem printing a two dimensional boolean array
    By sallyB in forum What's Wrong With My Code?
    Replies: 10
    Last Post: June 23rd, 2011, 03:56 PM
  3. Two-dimensional boolean array?
    By tcmei in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 17th, 2011, 08:32 PM
  4. how to read a text delimited file using 2 dimentional array in java ??
    By pooja123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2011, 09:11 AM
  5. Printing boolean values from an array
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 18th, 2010, 12:11 AM