Jul 25, 2008

Compare Char's In Words

Free Web Hosting, No Ads > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

free web hosting

Compare Char's In Words

sonesay
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..

cat
dog
horse

Out 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

CODE
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?

 

 

 


Reply

CrashCore
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://java.sun.com/j2se/1.5.0/docs/api/index.html


You will want to change you code to something like:
CODE
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:
CODE
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.

 

 

 


Reply

sonesay
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.

CODE
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 again
Sone

Reply

CrashCore
QUOTE(sonesay @ Mar 9 2008, 12:15 AM) *
- 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://java.sun.com/j2se/1.5.0/docs/api/ja.../Character.html
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!

Reply

galexcd
QUOTE(CrashCore @ Mar 9 2008, 01:40 PM) *
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.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : chars words


    Looking for compare, chars, words

Searching Video's for compare, chars, words
advertisement



Compare Char's In Words



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE