aspose file tools*
The moose likes Java in General and the fly likes 2darray counter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "2darray counter" Watch "2darray counter" New topic

2darray counter

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
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 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?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14347

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 "" 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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146

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

Joined: Jul 14, 2006
Posts: 146
hey sorry i found out why it printed, 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

Joined: Jul 14, 2006
Posts: 146
Got it workin when i changed my if statement;

if (lines2[numRow].toString().contains("circle")) {
totCircle = totCircle + 1;
suppose by including exact reference in the string 2d array i was allowed compare the string element.
It is sorta covered in the JavaRanch Style Guide.
subject: 2darray counter