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

Thread: Printing boolean values from an array

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Printing boolean values from an array

    I am having difficulty with a particular segment of a project and have created a test class in an attempt to debug it. The project requires a multi-dimensional array consisting of boolean values which I set to false and then specific coordinates of that grid to true. I then have a method with an if statement that if that boolean value is true then do such and such. It works backwards. Trying to look into the cause of this I created the following class:
    public class booleanArrayTest {
     
    	/**
    	 * @param args
    	 */
     
    	public int n = 9;
     
    	public boolean[][] start = new boolean[n][n];
     
    	public void falseStart() {
    		for (int x = 0; x < n; x++) {
    			for (int y = 0; y < n; y++) {
    				start[x][y] = false;
    			}
    		}
    	}
     
    	public void displayStart() {
    		for (int x = 0; x < n; x++) {
    			for (int y = 0; y < n; y++) {
    		if (start[x][y] = false) {
    			System.out.println(start[x][y]);
    		}
    			}
    		}
    	}
     
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		booleanArrayTest ArrayTest = new booleanArrayTest();
     
    		ArrayTest.falseStart();
    		ArrayTest.displayStart();
     
    	}
    }
    Why can't I print those boolean values from the grid in this manner?

    Thanks in advance
    Last edited by copeg; November 17th, 2010 at 08:36 PM.


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,235
    Thanks
    176
    Thanked 817 Times in 760 Posts
    Blog Entries
    5

    Default Re: Printing boolean values from an array

    For future reference, please flank your code with the code tags. In addition, it helps to post any and all error messages you are getting. In this case, I presume its a type mismatch on the line
    if ( start[x][y] = false )
    which should be a boolean
    if ( start[x][y] == false)

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Printing boolean values from an array

    Thanks, I appreciate it. Now my program works fine, I had a feeling I was overlooking something obvious. Also, I actually was not getting an error message, I was getting the opposite of the results I was expecting when I ran my program.

    Thanks again,

    Deprogrammer

  4. #4
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    489
    Thanks
    8
    Thanked 45 Times in 43 Posts

    Default Re: Printing boolean values from an array

    I actually was not getting an error message, I was getting the opposite of the results I was expecting when I ran my program.
    This code
    if ( start[x][y] = false )
    assigns the boolean value false to start[x][y] and tests that value, which is (oh so obviously) false, and the if block is never entered.

    db

Similar Threads

  1. printing array recursively
    By kyros in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 19th, 2010, 12:04 AM
  2. Printing the Max and Min in an Array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2010, 08:28 AM
  3. Doubt Regarding Printing an Array List
    By reeceypp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2010, 03:11 AM
  4. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  5. Substitution of Values in Array
    By nyeung in forum Collections and Generics
    Replies: 2
    Last Post: October 26th, 2009, 08:02 PM