• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method that compares two String arrays  RSS feed

 
Rachell Zammit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am studying for a final first year Java exam and there is a question in a past paper I can't work out. Can anyone give me some hints as to how to go about it?

The question is:

a. (6 marks) Write a method for a Java class which when passed two arrays of strings x and y, counts the number of times each string occurring in y occurs in x and prints the results in the order the strings appear in y. For example, if x = {"ab", "cb", "ab", "dc", "cb"} and y = {"ab", "dc", "ef"}, then the program should output: ab: 2 dc: 1 ef: 0



 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you just have to loop through the string array and compare the each elements through equals() and increment the counter accordingly.
Can you show the code that you tried..?
 
Rachell Zammit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi thanks, problem is how to 'separate' the count for the elements and how to compare them properly.

The two methods I have below are wrong and/or incomplete.








 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to reset your count variable each time the outer loop increments.

What's the purpose of "meth2"? And please try to come up with better names for methods.

It is possible to do what you want to accomplish with one method (getting the count and printing it to the screen).
This could be done by adding the line:
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look this code.. I have made a skelleton for you.. Includes some varaibles and println to complete it..



Hope it helps.
 
Aj Prieto
Ranch Hand
Posts: 75
Android Chrome Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ishan Pandya wrote:Look this code.. I have made a skelleton for you.. Includes some varaibles and println to complete it..



Hope it helps.


First off, that code wouldn't compile.
There is no .length() method in the array class and your variable "y" is already defined in your parameters, so you can't use that in your "for" loop.
Overall, the skeleton you provided seems way more complex than what is required.

@Rachell
You're meth1 looks closest to being correct. You just have to reset the count variable and I would print the result before closing the first "for" loop.
 
Rachell Zammit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Aj Prieto Thanks for clearing that up I was confused after reading Ishan Pandya code, not to say I don't appreciate the help Ishan! But yes the length() method being used along with the extra for loop was confusing me.

Cheers.

 
Rachell Zammit
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally got it working!

Thank you

Outputs:
ab 2
dc 1
ef 0
[Finished in 0.6s]








 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
red faced.. sorry for that..
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know its quite late. But to the OP who was confused by my code i have wrote a more optimized code using collections.



I hope you go through it once.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!