Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: Boolean Arrays true/false switch

  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Boolean Arrays true/false switch

    I'm totally lost. I'm suppose to have make an Boolean Array which displays false originally and then alternate between true & false;

    For example:
    arr[0] = true;
    arr[1] = false;
    arr[2] = true;

    Then I'm suppose to print in void printBoolean(boolean[] arr)

    Can somebody veer me in the right direction?

    Java Code:
    public class Boolean {
    
    	void fillBoolean(boolean[] arr) {
    		// fillBoolean will fill the arr array passed in with alternating true false values.
    		
    		 boolean[] trueFalse = new boolean[] {true};
    		 
    		 int n, i = n*2;
    		
    		 for (n = 0; i < trueFalse.length; n++) {
    			 trueFalse[i] = !trueFalse[i];
    			}
    		 
    	}
    	void printBoolean(boolean[] arr){
    		
    		// The printBoolean routine will print out the contents of the array arr passed in.
    		
    		int index;
    		
    		for (index = 0; index + 1 <= 20; index++)
                System.out.println(fillBoolean(arr)[index]); 
        }
    		
    	public static void main(String[] args) {
    
    	}
    
    }

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    1. Don't initialize your array in fillBoolean, the array is passed by the caller.
    2. That's not how arrays are initialized ... Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    3. That link will also address why System.out.println(fillBoolean(arr)[index]); -> System.out.println(arr[index]));
    4. I assume the array should be initialized in main then printed? then passed to fillBoolean, then (re-)printed ... if so void -> static void on both of your functions.
    5. The class name Boolean is bad... Boolean already exists in java, and is kinda important.
    Last edited by SJF; 03-19-2013 at 09:52 PM. Reason: link not like

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Boolean Arrays true/false switch

    Quote Originally Posted by SJF View Post
    Really?

    Java Code:
    boolean arr[] = new boolean[3];
    arr[0] = true;
    arr[1] = false;
    arr[2] = true;
    
    // or
    
    int[] intArray = new int[]{1,2,3,4,5,6,7,8,9,10};
    boolean [] boolArray = new boolean[] {true, false, true, false};
    Works for me.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Yeah, ok, so you might do that... if no. DON'T DO THAT.

    boolean[] trueFalse = new boolean[] {true};

    is a one member array.

    not really useful and trueFalse[ANYTHING BUT 0] = no good.

    boolean[] trueFalse = new boolean[] {false,false,false,false,false,false,false,false,f alse,false,false,false,false};

    could work... but really?

  5. #5
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    But anyway Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics) default boolean = false,

    so
    Java Code:
       boolean[] bool = new boolean[400];  // is a new 400 element false array.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Boolean Arrays true/false switch

    I'm not certain of the point you are making. He initialized his array to true.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: Boolean Arrays true/false switch

    The issue was not whether he was initializing to all false values or whether it was a 1 x1 array. Or whether his/her code is optimal. This issue was whether s/he could initialize that way. You said no. I said yes.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Quote Originally Posted by LetsG0Blue View Post
    .... make an Boolean Array which displays false originally ....
    Was all I was addressing.

  9. #9
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    How do I get the value to appear? I.E. the output

    arr[0] = true
    arr[1] = false
    arr[2] = true

    Java Code:
    public class Boolean {
    
    	void fillBoolean(boolean[] arr) {
    		// fillBoolean will fill the arr array passed in with alternating true false values.
    		 
    		 int n = 1, i = n*2;
    		
    		 for (i = 1; i < arr.length; i++) {
    			 arr[i] = !arr[i];
    			}
    		 
    	}
    	void printBoolean(boolean[] arr){
    		
    		// The printBoolean routine will print out the contents of the array arr passed in.
    		
    		int index;
    		
    		for (index = 0; index + 1 <= 20; index++)
                System.out.println(arr[index]); 
        }
    		
    	public static void main(String[] args) {
    
    	}
    
    }

  10. #10
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Well, your printBoolean() should do the trick. Create your array in the main function and call printBoolean(yourArray) (You'll have to declare printBoolean static first.)

  11. #11
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    I'm still real new at this and haven't had much time to practice. How would I go about calling my printBoolean?

    Java Code:
    public class Boolean {
    
    	void fillBoolean(boolean[] arr) {
    		// fillBoolean will fill the arr array passed in with alternating true false values.
    		 
    		 int n = 1, i = n*2;
    		
    		 for (n = 1; i < arr.length; n++) {
    			 arr[i] = !arr[i];
    			}
    		 
    	}
    	static void printBoolean(boolean[] arr) {
    		
    		// The printBoolean routine will print out the contents of the array arr passed in.
    		
    		int index;
    		
    		for (index = 0; index + 1 <= 20; index++)
                System.out.println(arr[index]);
    			System.out.println(arr);
        }
    		
    	public static void main(String[] args) {
     
    		boolean[] arr = new boolean[] {true};
    			     
    			
    
    	}

  12. #12
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Java Code:
        printBoolean(arr);

  13. #13
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    I'm getting this error.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
    at Boolean.printBoolean(Boolean.java:21)
    at Boolean.main(Boolean.java:28)

    true
    it has something to do with

    Java Code:
    	static void printBoolean(boolean[] arr) {
    		
    		// The printBoolean routine will print out the contents of the array arr passed in.
    		
    		int index;
    		
    		for (index = 0; index + 1 <= 20; index++)
                System.out.println(arr[index]);
        }
    Java Code:
    public class Boolean {
    
    	void fillBoolean(boolean[] arr) {
    		// fillBoolean will fill the arr array passed in with alternating true false values.
    		 
    		 int n = 1, i = n*2;
    		
    		 for (n = 1; i < arr.length; n++) {
    			 arr[i] = !arr[i];
    			}
    		 
    	}
    	static void printBoolean(boolean[] arr) {
    		
    		// The printBoolean routine will print out the contents of the array arr passed in.
    		
    		int index;
    		
    		for (index = 0; index + 1 <= 20; index++)
                System.out.println(arr[index]);
        }
    		
    	public static void main(String[] args) {
     
    		boolean[] arr = new boolean[] {true};
    		
    			printBoolean(arr);
    			
    
    	}
    }

  14. #14
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Your for loop in printBoolean is looking for a boolean array of length 20. instead of (index = 0; index + 1 <= 20 ..... -> (index = 0; index < arr.length; index++) just like the for loop in fillBoolean.
    Speaking of which .. (n=1; i < arr.length; n++) I don't think that's what you want... (but the i < arr.length is probably right, just not the n business)

  15. #15
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Quote Originally Posted by SJF View Post
    Your for loop in printBoolean is looking for a boolean array of length 20. instead of (index = 0; index + 1 <= 20 ..... -> (index = 0; index < arr.length; index++) just like the for loop in fillBoolean.
    Speaking of which .. (n=1; i < arr.length; n++) I don't think that's what you want... (but the i < arr.length is probably right, just not the n business)
    I don't believe it's right either, but I need every other array to print out true..

    Example:

    true
    false
    true
    false

  16. #16
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    If you get your OOB Exception to go away. (i < array.length is your friend). Your printBoolean will work.

    Now then: when boolean (primitives) are initialized they are false by default. Enter fillBoolean(). So... every other entry should be false... a[i] = !a[i-1] ? Maybe? Note: array[-1] will also get you an OOB Exception.

  17. #17
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Quote Originally Posted by SJF View Post
    If you get your OOB Exception to go away. (i < array.length is your friend). Your printBoolean will work.

    Now then: when boolean (primitives) are initialized they are false by default. Enter fillBoolean(). So... every other entry should be false... a[i] = !a[i-1] ? Maybe? Note: array[-1] will also get you an OOB Exception.
    Doesn't work still returns just false ten times... wish my professor actually went over this material during his lectures.
    Mickeycricky likes this.

  18. #18
    Mickeycricky is offline Member
    Join Date
    Feb 2013
    Posts
    1
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    I've got to do this same problem. It's very confusing.

  19. #19
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    Look: There are two ways to get what you want. (Well more than that, but here are two)

    First:
    1. Set first item in array to true -> arr[0] = true;
    2. Use a for loop (from 1 to 9) and set each array item to NOT it's predecessor -> arr[i] = !arr[i-1]

    Second:
    1. Go through the loop from 0 to 9 and set each even item true (they are false to begin with). if( i % 2 == 0)

    .... or go through loop from 0 to 8 ; i+=2 setting arr[i] to true....

    But steps in main are
    A. initialize array.
    B. call fillBoolean(yourArray)
    C. call printBoolean(yourArray)

  20. #20
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Boolean Arrays true/false switch

    check your for loop
    Java Code:
       for (index = 0; index + 1 <= 20; index++)
                System.out.println(arr[index]);
        }
    this got issue.

    Refer this link:ttp://stackoverflow.com/questions/4958235/why-does-arrayindexoutofboundsexception-occur-and-how-to-avoid-it-in-android

    Finally you put correct value for array index range
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 09-09-2012, 04:35 PM
  2. Replies: 3
    Last Post: 09-13-2011, 12:01 AM
  3. Replies: 2
    Last Post: 05-25-2011, 02:33 PM
  4. how to balance true and false instances per id ?
    By aneuryzma in forum New To Java
    Replies: 1
    Last Post: 03-27-2011, 02:35 PM
  5. Prime Number - true , false
    By pinkdreammsss in forum Java Applets
    Replies: 11
    Last Post: 05-04-2010, 02:49 PM

Posting Permissions

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