Jump to content
xisto Community
Sign in to follow this  
sonesay

Compare Char's In Words

Recommended Posts

I've got an exercise given out by my tutor and I'm basically stumped. Part of it is to compare words then work out the shortest number of characters that is required to uniquely identify them. For example..catdoghorseOut of this group of only the first letters can be used c, d, h where as other groups like ant and antelope would require ant, ante.Given this problem I've been trying to figure out how to do this and so far I'm manage to split each word into char arrays but when I try to compare them with other copies for a match I get two errors, Either I get incompatible data types

char[] cLetters = word.toCharArray();            //System.out.println(cLetters.length);            for(char letter : cLetters)            {                if(letter == "c");            }

or I get char cannot be dereferenced. I dont know how to use them exactly so I need some help, I have been looking at the API but theres so many methods my eyes are gone blurry lol. I've got to be able to compare each letter to another, whats the method to do that?

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

In order to use the "for each" loop, you need to be referencing an object which can return an iterator and, therefore, implements iterable. Since char is a primative datatype, it will not be iterable unless it is in some sort of collection (and an array is also primative, so it won't work either).


Anyhow, you can view the javadoc information for iterator here:

http://docs.oracle.com/javase/1.5.0/docs/api/index.html


You will want to change you code to something like:

Character[] cLetters = word.toCharArray();LinkedList<Character> cLetters2 = new LinkedList<Character>(cLetters);   //System.out.println(cLetters.length);   for(Character letter : cLetters2)   {	  if(letter == "c");   }

Otherwise, the even simpler solution would be use it just as is, and not use the iterator for the foreach loop:
char[] cLetters = word.toCharArray();   //System.out.println(cLetters.length);   for(int i=0; i<cLetters.length; i++)   {	  if(cLetters[i] == "c");   }

Hope this helps! If you need anything else, just ask.

Notice from rvalkass:

Added code tags around the code.

Share this post


Link to post
Share on other sites

Thanks for the info. I ditched the use for chars for this problem since I found a method in the string class to handle such check. This is after hours of course of trail and error until I eventually came across it. The java libraries are insanely huge lol there is practically so many ways to do things.

letterMatch = word1.regionMatches(true, position, word2, position, 1);

That seem to work ok for me, letting me check each character in both the string arrays. I will no doubt check back here again if I need the code for handling characters.- Question on characters. is there a difference between char[] and character[] ?- When comparing certain strings and chars I notice it didnt seem to work when I compare == '' vs == "". The first single quotes seems to work for char but double quotes don't.Thanks againSone

Share this post


Link to post
Share on other sites

- Question on characters. is there a difference between char[] and character[] ?

 

- When comparing certain strings and chars I notice it didnt seem to work when I compare == '' vs == "". The first single quotes seems to work for char but double quotes don't.

1. Yep, but you have to spell it like this: Character (javadocs for this class at:

http://forums.xisto.com/no_longer_exists/

The difference is that char is a primitive datatype (that is, it is not an object, but merely a value). The other version, Character (with the capitalization), is actually a real java Object (that is, it has a value and methods associated with it).

 

2. I believe the problem is this: Single quotes denote characters (e.g. 'a' and '6'). However, if you use " " you are referring to a string, not a character (e.g. "5" OR "Hello this is a string"). Also, strings cannot be compared (in java!) with the == operator (well, it would just compare the physical address, which I don't believe Java allows). To compare two strings, you need to use the syntax: string1.equals(string2) [this returns a boolean]

 

Hope this helps with the questions!

Share this post


Link to post
Share on other sites

2. I believe the problem is this: Single quotes denote characters (e.g. 'a' and '6'). However, if you use " " you are referring to a string, not a character (e.g. "5" OR "Hello this is a string"). Also, strings cannot be compared (in java!) with the == operator (well, it would just compare the physical address, which I don't believe Java allows). To compare two strings, you need to use the syntax: string1.equals(string2) [this returns a boolean]

Arghh yess this is why I hate java. They could have bothered to incorporate strings a little bit more than just add it as a class. Sure some of the string methods are useful, but its not the same.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.