Results 1 to 8 of 8
  1. #1
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default Printing value of boolean arrays

    Hi. How do I print the value of an array. I know it has something to do with the toString method...

    I wrote:

    Java Code:
    public static void main(){
       boolean [] a = {true, true};
       System.out.println(a);
    }
    I want true, true to appear as my output. Right now, there's jibberish on the terminal window screen. I'm using blueJ.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    6

    Default

    Hi, there are a few options:
    Java Code:
    boolean [] a = {true, true};
    System.out.println(Arrays.toString(a));
    for (boolean b : a) {
    	System.out.println(b);
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    I'm not allowed to use class Array.

    I remember there's something about when you do System.out.println(variable) it checks the toString method of the variable

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by myst View Post
    Right now, there's jibberish on the terminal window screen.
    It's not gibberish; if a class doesn't override the toString( ... ) method (and arrays don't do that) Objects version is called which prints the canonical name of the class, followed by an at sign followed by the hashCode() of the object. On my system your array prints as: [Z@3ce53108, which means an array ([) of booleans (Z).

    kind regards,

    Jos

  5. #5
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    It's not gibberish; if a class doesn't override the toString( ... ) method (and arrays don't do that) Objects version is called which prints the canonical name of the class, followed by an at sign followed by the hashCode() of the object. On my system your array prints as: [Z@3ce53108, which means an array ([) of booleans (Z).
    Oh, cool.

    So how do I apply the toString() method here?

    I know it goes something like this:

    public String toString(){
    String s="";
    s+="true, true";
    return s;
    }

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by myst View Post
    So how do I apply the toString() method here?
    You can't add methods to an array.

    kind regards,

    Jos

  7. #7
    myst is offline Member
    Join Date
    May 2010
    Posts
    44
    Rep Power
    0

    Default

    So can you tell me how I can get true, true as my output when I write S.o.p(a); ???

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,998
    Blog Entries
    7
    Rep Power
    19

    Default

    Quote Originally Posted by myst View Post
    So can you tell me how I can get true, true as my output when I write S.o.p(a); ???
    You can't do it that way; read what PhHein wrote.

    kind regards,

    Jos

Similar Threads

  1. use boolean as 0 or 1
    By joost_m in forum New To Java
    Replies: 10
    Last Post: 04-13-2010, 11:22 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. Boolean problems
    By Chasingxsuns in forum New To Java
    Replies: 2
    Last Post: 09-15-2009, 10:57 PM
  4. Simple Boolean
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-01-2009, 05:01 AM
  5. Replies: 21
    Last Post: 11-05-2008, 02:31 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
  •