Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Compare Char's In Words
sonesay
post Mar 9 2008, 12:42 AM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 640
Joined: 20-June 07
From: Auckland
Member No.: 45,102



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?

This post has been edited by sonesay: Mar 9 2008, 12:43 AM
Go to the top of the page
 
+Quote Post
CrashCore
post Mar 9 2008, 03:55 AM
Post #2


Super Member
*********

Group: [HOSTED]
Posts: 225
Joined: 8-April 05
Member No.: 5,385



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.
Go to the top of the page
 
+Quote Post
sonesay
post Mar 9 2008, 05:15 AM
Post #3


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 640
Joined: 20-June 07
From: Auckland
Member No.: 45,102



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
Go to the top of the page
 
+Quote Post
CrashCore
post Mar 9 2008, 09:40 PM
Post #4


Super Member
*********

Group: [HOSTED]
Posts: 225
Joined: 8-April 05
Member No.: 5,385



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!
Go to the top of the page
 
+Quote Post
galexcd
post Mar 10 2008, 05:41 AM
Post #5


Define:EVIL PROGRAMMER (ē'vəl prō'grăm'ər)- n. An organism that converts caffeine into evil software.
*********

Group: [HOSTED]
Posts: 974
Joined: 25-September 05
From: The dungeon deep below the foundation of trap17
Member No.: 12,251



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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Spellings Are Not Important.(34)
  2. Ati Updates Drivers Frequently Compare To Nvidia(9)
  3. Words Of Wisdom(26)
  4. Making A Scroll Bar In Flash(4)
  5. Formal Spanish Words(14)
  6. Your First Words(4)
  7. Keeping Your Email Address Out Of The Hands Of Spam Artists(5)
  8. Your View And Definition Of Swear Words.(10)
  9. "i Love You"(11)
  10. Internet Jargons And Their Definitions(3)
  11. Learn With Pictures, Not Words.(13)
  12. Google Add Words(2)
  13. A Fortunate Accident(0)
  14. Adsense In A Few Words(3)
  15. Stuck For Words(1)
  1. Good Advice For Everybody(4)
  2. Compare Two Vars And Highlight The Differences(7)
  3. Sos...sos...help Me Pls.(1)
  4. Phpnet(6)
  5. What Are Acceptable Words In My Forum Signature?(5)
  6. Persian (farsi) Languages(0)
  7. Folksonomy(1)
  8. Compare 2 List Of User Ids From Different Tables(1)
  9. No Words?(13)
  10. Seo = Just Remember To Keep Your Meta Tag To 3 Key Words(0)
  11. Hello! (these Words Need Your Attention)(4)
  12. Client Correspondence - Keeping Unflattering Words Off The Record(0)
  13. Awardspace Is So Limited Compare To..(2)


 



- Lo-Fi Version Time is now: 20th July 2008 - 08:25 PM