aspose file tools*
The moose likes Beginning Java and the fly likes comparing an element in one array with all the elements in another array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "comparing an element in one array with all the elements in another array" Watch "comparing an element in one array with all the elements in another array" New topic
Author

comparing an element in one array with all the elements in another array

Rach Glover
Greenhorn

Joined: Jun 23, 2007
Posts: 5
Hello everyone.
I'm trying to set up a method which takes an element from one char array and checks to see if matches any of the chars in another char array. I think i've spent so much time on it now that I've completely confused myself! Here is what I have so far:



The idea being that it takes the first character in "checking", checks it against all of the letters in "valids" and if the letter isn't found in "valids", it says that it isn't a valid letter (in due course, i'm going to throw an exception). However, at the moment it tells me that each letter that doesn't match the one currently being checked is invalid!

I'm pretty sure the problem is the way that I've set up how it checks. I've tried using a for-each loop instead of the second for loop, but I end up with an ArrayIndexOutOfBoundsException.

Can anyone help or point out where I'm going wrong?

I hope I've explained it in enough detail so you get what I mean.

Cheers,
Rach
Rach Glover
Greenhorn

Joined: Jun 23, 2007
Posts: 5
Just re-reading through my post and I thought I had better make it a bit clearer what the output is that I'm getting. I modified it slightly so that rather than System.out.println(i + " is a valid letter), I have System.out.println(checking[i] + " is a valid letter) so I get the letter rather than the index.

When I run the program I get the following:
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy Rach,

this seems to be your first posting here so

Welcome to the Ranch!

Considering your method, you have one syntax problem and two logical problems.

Syntax: your method
public String check() { ...

says that it will return a String, but it doesn't. This won't compile. Either turn the return type from String to void or return a String at the end of the method. If you won't bother at the moment what you can return, you can also code quick and dirty
return null;
at the last line of the method. But don't get acustomed to it...

Best for the moment would be set the return type to void.



logical 1: the method prints out the index number i, and not the character of the "checking" array at position i.

logical 2: the method compares every checking element with every valids element. That is so far correct. But it outputs the right or wrong message for every pair. And that is not correct.
E.g it compares A with A first (output "A is valid") but in the next cycle it compares A with B and says "not valid".

Better assume for every "cheching" character, that it is not valid in the first place. Then if there is a match in the innermost loop, make a record that IS valid. For each "checking" character then output this record, when the checking letter has been compared to all of the valid letters.
You'll need another variable for the record.

Hope that helped so far.

Yours,
Bu.


all events occur in real time
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Hi Rach.

First of all you method can compile as you posted.. if you declare it to return a String value, you have to return one. Maybe NULL if you prefer. Probably you simply posted just a part of it.. in that case, it's ok. Just a suggestion: for the name you chose for the method (check) I'd better return a boolean value...anyway, personal choices.

To answer you question... I can figure out 2 different ways: the one you followed so far, the other one which assumes you know something about Collections.

In the second case, all you have to do is to put the values contained in the valids array in a data structure implementing the Set interface. LinkedHashSet could be a good candidate. If you do so, you can reduce you method to a single line of code calling the Set mehod contains. Be carefull if you decide to use the same tecnique with "custom" objects... In that case you have to implements for those objects the equals() and hashcode() method in a convenient way.

Second way, yours. First of all I would split your method in 2 parts. The inner cycle is the one who really makes the comparison. You can lanuch somewhere else a cycle which loops throw the checking[] calling for each element the previous announced method. Any way, whatever you decide, right before starting the inner cycle you should declare a boolean variable which everytime take note of the comparison between the 2 chars. Every time you do a new comparison for the same "to-be-tested char", you update the value of that variable doing the OR with the old value and the new result.

Little hint..maybe your case is some kind of validation which requires you study a bit of RegularExpressions?

Hope being of help,
cheers.


SCJP 5 & 6, SCWCD 5, SCBCD 5
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Sorry, didn't see your second posting before posting for myself. But I think you should get the correct answer before I'm back.
Now of for the shop, my java is out and filters I don't have either.

By,
Bu.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
No, don't study regex (regular expressions) at this moment.

I think this is about basic programming, and regex - surely could solve the task but - is at this point useless.

Bu.
Rach Glover
Greenhorn

Joined: Jun 23, 2007
Posts: 5
Thank you both for your replies

I forgot to type in the very end of the code where I have a "return toString();" into the code above - doh!

I've been trying to implement the booleans into my loops, but I need 'true' to override 'false'. Is that what you meant by using OR Bianchi?

Here's what I have now:


I'm getting an error message regarding the last 'if' (Cannot find symbol variable isValid)...hmmm this has thrown me a bit.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Back with 2*250 g of Coffee for €3.29.


Your variable "isValid" is out of scope. It can be accessed only within the block (and nested blocks) were it has been declared.
You try to access it from outside, where it doesn't exist.

Bu.
Rach Glover
Greenhorn

Joined: Jun 23, 2007
Posts: 5
wow, I wish I could get coffee for that price in Britain!!

I'm playing around with the booleans at the moment and I'll let you know when I've got it cracked

One more question (if you don't mind...): Why does this code result in an ArrayIndexOutOfBoundsException if I try to make the for loop containing "valids" a for-each loop? Just out of interest so I can look out for it in future.

Thanks again,
Rach
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi Rach,

perhaps have a cup of tea then.

With your for-each loop I don't know. When I make it up as
it behaves exactly as your original code. Do you still have a version of your exception throwing method?

Bu.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Rach Glover:
...One more question (if you don't mind...): Why does this code result in an ArrayIndexOutOfBoundsException if I try to make the for loop containing "valids" a for-each loop? Just out of interest so I can look out for it in future. ....



You have used the   ==   operator, you should be using dot equals method call, probably of the String class. Loop goes for a (ahem...) while "()?" then blows Krakatoah becase JAVAC has collapsed the string literals and the == works until you get to "D" {},... thats the only thing I can spot becuse I went looking for a classic off by one error in loop indexing and it did not seem to be there.

Consider


The Collections usually have rich tools for this type of work and can be good idea farms for thoughts you will have.



"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Francesco Bianchi
Ranch Hand

Joined: Jun 22, 2007
Posts: 62
Originally posted by Nicholas Jordan:



You have used the   ==   operator, you should be using dot equals method call, probably of the String class.



Not completely sure about this but...I think the operator == works well with primitive types...and chars are of that kind.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
it wouldn't even compile.
equals () is a method, and primitive types don't have methods.

Yours,
Bu.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Bianchi Francesco:
Not completely sure about this but...


You are correct, Burkhard Hassel spotted this also.

Re-reading the Origial Poster's question, we should approach the matter from the perspective of later re-invent the wheels that will slow progress.

First off, in the search loop that returns true for if( == ), we place a break. Second, OP should do nothing in the else to attain desired behaviour. Third, original poster could use an array of:

Which would provide equals methods. Rach Glover is in need of coffee right now, telling us that each letter that doesn't match the one currently being checked is invalid - thus the obvious solution just within reach.
Rach Glover
Greenhorn

Joined: Jun 23, 2007
Posts: 5
Hello,

Thanks so much for all your input

This is what I've done and it seems to work how I wanted it to, if a little long and messy!


It works the way I want it too, although I'm sure there's probably a better way! Right now, though, I'm happy with what I have as it does the job.

Thanks again for all the pointers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: comparing an element in one array with all the elements in another array
 
Similar Threads
array confusion
Array without ints
Counting chars during file read.
Sun Cirtification
My head is about to explode!