Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to compare two values of an two array2

 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all, this is indeed a homework question (I read the policy.) But I've tried everything and searched the web for ideas or functions with no luck.
Anyways, the basic part that I'm stuck at is I'm unable to find a way to count a number of times a character is in a string. like:

string blarg = "aabbccc"
counting the number of "b" would be 2

Now... the problem is Java doesn't like Strings versus Arrays, (unlike C/C++ where they are the same thing and I wouldn't be asking this question.)

So I tried converting it into an array, which worked, but then I was unable to count it. ( I've tried split and string.toCharArray)
count wise, using for loop and length of array. That part is not what I'm asking....

Pretty much I'm trying to find a command/method where I can compare two values of two separate arrays.

equals ( arr1 , arr 2) didn't work for me...
the == doesn't work for objects....

Maybe I'm not making them as objects?

Some points or hints will be helpful.
Thanks-
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think there is a generic way to compare two elements in arrays like that. how you would compare elements in an array of Integer objects would be completely different from how you would compare elements in an array of FooBar objects.

I guess the question I'd ask is "how do you want do to it"? you say you converted it to an array...but an array of WHAT? And then why were you unable to count it?

What kind of thing does your array hold? If they are objects, you would generally compare them like this:

arr1[0].equals(arr2[0])

that says "call the equals() method of the first object in arr1 and pass it the first object in arr2". If the equals method is written correctly, that should return true/false appropriately.

If your array contains primitives, you can just do

arr1[1] == arr2[1]

 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I asked another CS friend and he said something similar to what you said. I was mistaken that I could not use the == for arrays in CERTAIN situations.

I had the idea of objects and elements confused. I'm going to re-explain this in case someone else stumbles upon my problem and to make sure I'm getting this right...

Char[] arr1 = "Yo!"
Char[] arr2 = "Yo!"

You can't use - if( arr1 == arr2 ) - In this cause you'd need the .equals
But you CAN use - if( arr1[0] == arr2[0] )

 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Chaung wrote:. . . You can't use - if( arr1 == arr2 ) - In this cause you'd need the .equals
But you CAN use - if( arr1[0] == arr2[0] ) . . .
You have misunderstood your friend, I am afraid.

Use == only for primitives and members of an enum. You need the equals method for all reference types, which includes arrays and their non-primitive elements. But if you look in the Java Language Specification, you find the only overridden method in arrays is clone(). So myArray.equals(myOtherArray) uses the == operator internally. If you want another version of equals() look in the Arrays class. I can't link to the method directly, because of the [] in it.

And welcome to the Ranch
 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the welcome...

Oh and I'm forbidden to use that class in this assignment or at least discouraged to use it. ^___^;;;;;
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Chaung wrote:
But you CAN use - if( arr1[0] == arr2[0] )

it depends on what is IN the array. If they are primitives, this works fine. If they are objects, this will NOT work.
 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah I JUST got the definition of primitives.... and I'm back to ground zero on the idea of counting the number of a certain character into a string...
So I has string blarg... convert it to a char array...

use charat to compare a element the char[] blarg to a character... <-- this is where I'm, stuck at.... again.... because it doesn't like the fact that the character "a" is a string. Would I then also convert that to a chararray?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's kind of hard to tell you what you're doing wrong without you posting your code. we can only guess what you are doing, and odds are we are going to guess wrong.

If you have converted your String to a char[], then you don't need to convert that to a char again.

The only thing i can GUESS that you are doing is something like

myArray[1] == "a"

and yes, that will not work, because you are creating a String. if you want to compare a char to a char, you need to use single quotes:

myArray[1] == 'a'

 
Tim Chaung
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:it's kind of hard to tell you what you're doing wrong without you posting your code. we can only guess what you are doing, and odds are we are going to guess wrong.

If you have converted your String to a char[], then you don't need to convert that to a char again.

The only thing i can GUESS that you are doing is something like

myArray[1] == "a"

and yes, that will not work, because you are creating a String. if you want to compare a char to a char, you need to use single quotes:

myArray[1] == 'a'



I love you. said the random stranger on the internet. Thank you so much for clarifying this.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic