• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

2darray counter

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,

me again!

Right so heres where im at now, new but similar prog.
I read in csv file;
store in ArrayList;
Convert arraylist toArray;
split commas and store in new Array;
build 2dArray and populate with it with new Array
eg looks like
circle red 1.22
square blue 2.33
circle red 2.11 and so on

Now i want to loop through this array, identify a circle in the array, and add it to a Total count of circle in the array, so above would end up totalCircle = 2;
So far ive done this;

it compiles but prints
java.io.BufferedReader@10b62c9 0 and 0 and 15
can you please tell me what im doing wrong, also my first and second if else statements are different but both work, what do ya think is best method?
Thanks
Mark
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You didn't add it in the code you posted above, but I guess that the variable lines2 is an array of arrays, right? If it isn't, your code wouldn't compile.

Ok. Look at this line:

if (lines2[numRow].toString().contains("circle")) {

Think about this: What kind of object is lines2[numRow]?
- It is an array; it is not a String containing a line of text.

What does lines2[numRow].toString() produce?
- It produces a String that looks like this: [Ljava.lang.String;@9304b1

Now your program is checking if the string "[Ljava.lang.String;@9304b1" contains "circle".
It doesn't, ofcourse, so totCircle is not incremented.

With the next if: else if (Square.equals(lines2[numRow])) { something else happens. Here you are comparing the String object that variable Square points to with a String array. Because a String array is not a String, String.equals(...) always returns false.

I don't know why "java.io.BufferedReader@10b62c9" gets printed on your output. The code you posted doesn't do that, it's probably somewhere else in your code.
[ July 27, 2006: Message edited by: Jesper Young ]
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey

Yea lines2 is a array of arrays


Yea i think i understand what you mean, so with that in mind will i have to change my array or is there some other approach i can take?
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey sorry i found out why it printed java.io.BufferedReader@10b62c9, i was trying to print rd from BufferedReader rd to see what would happen, sorry my bad. But also you were right about the (lines2[numRow].toString().contains("circle"), it prints similar message.
Im now unsure how to proceed
 
Mark Hughes
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it workin when i changed my if statement;
To;


from:
[code}
if (lines2[numRow].toString().contains("circle")) {
totCircle = totCircle + 1;
}
[/code]
suppose by including exact reference in the string 2d array i was allowed compare the string element.
Thanks
Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic