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:
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!/** * 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 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