Results 1 to 8 of 8
Thread: Printing value of boolean arrays
- 06-02-2010, 07:29 PM #1
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
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:
I want true, true to appear as my output. Right now, there's jibberish on the terminal window screen. I'm using blueJ.Java Code:public static void main(){ boolean [] a = {true, true}; System.out.println(a); }
- 06-02-2010, 07:42 PM #2
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
- 06-02-2010, 07:54 PM #3
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
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
- 06-02-2010, 08:00 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,421
- Blog Entries
- 7
- Rep Power
- 17
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
- 06-02-2010, 08:03 PM #5
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
Oh, cool.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).
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;
}
- 06-02-2010, 08:10 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,421
- Blog Entries
- 7
- Rep Power
- 17
- 06-02-2010, 08:13 PM #7
Member
- Join Date
- May 2010
- Posts
- 44
- Rep Power
- 0
So can you tell me how I can get true, true as my output when I write S.o.p(a); ???
- 06-02-2010, 08:44 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,421
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
use boolean as 0 or 1
By joost_m in forum New To JavaReplies: 10Last Post: 04-13-2010, 11:22 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Boolean problems
By Chasingxsuns in forum New To JavaReplies: 2Last Post: 09-15-2009, 10:57 PM -
Simple Boolean
By jigglywiggly in forum New To JavaReplies: 3Last Post: 01-01-2009, 05:01 AM -
[SOLVED] Help with arrays, printing highest and lowest value of the array.
By Sophiie in forum New To JavaReplies: 21Last Post: 11-05-2008, 02:31 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks