• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 62
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Francesco Bianchi
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it wouldn't even compile.
equals () is a method, and primitive types don't have methods.

Yours,
Bu.
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
The only thing that kept the leeches off of me was this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic