May 12, 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:

Recent Queries:-
  1. char * compare c - 270.01 hr back.
  2. compare chars - 1074.79 hr back.
  3. compare two string word - 1077.01 hr back.
  4. char cannot be dereferenced - 1119.18 hr back.
  5. linkedlist - 1119.58 hr back.
  6. c programming check single quote char - 1122.18 hr back.
  7. java character compare string - 1122.36 hr back.
Similar Topics

Keywords : compare, chars, words

  1. Client Correspondence - Keeping Unflattering Words Off The Record
    (0)
  2. Hello! (these Words Need Your Attention)
    (4)
    Hi guy. slave here.. i'm from japan. yokohama city /smile.gif" style="vertical-align:middle"
    emoid=":)" border="0" alt="smile.gif" /> studying ...... doing master in Business (MIS) I'm new
    in this forum /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> i
    guess will have a good time here.. The work i love to do killing my time /tongue.gif"
    style="vertical-align:middle" emoid=":P" border="0" alt="tongue.gif" /> watching movies.. Making
    website /wink.gif" style="vertical-align:middle" emoid=";)" border="0" alt="wink.gif" /> ....
  3. Seo = Just Remember To Keep Your Meta Tag To 3 Key Words
    (0)
    i am editing this post because i meant to reply to the nine ways to promote your site and i think i
    accidently posted a new topic i guess. the thread i was replying to was...
    http://www.trap17.com/forums/9-ways-promote-site-t43286.html i hope this makes sense now
    /smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> to reread
    lyons post on the first page(which was posted over a year ago i think). seo changes constantly and
    what works today will probably not work tomorrow. why? because people will always abuse they way
    things work and t....
  4. No Words?
    Question.... (13)
    Ok I made an SMF forum after all with my hosting account, and installed some mods on it and all
    that, but why won't words appear on it!!!! Like it will say something like: :
    (0) instead of Total Trade Count : (0) and there will be text areas with no words next to
    it.....just wondering what I'm doing wrong lol.... And also some things like links at the top
    and such......
  5. Compare 2 List Of User Ids From Different Tables
    (1)
    Hi all. I am trying to make a list out of 2 list. The first list is a complete list of users id
    from users table (List A) The Second list is of users id from another table (List /cool.gif"
    style="vertical-align:middle" emoid="B)" border="0" alt="cool.gif" /> I want to subtrack the user
    ids in List B from List A and make List C. Thanks in advance for any help /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Update: I looked up a old
    sql book i had and found some query examples. I tried the EXCEPT statement in mysql but it dont wor....
  6. Folksonomy
    Folksonomy -- Folks-Taxonomy or in other words "Peoples classifica (1)
    This term is not so popular, but the concept(Collaborative Tagging) behind is widely used nowadays.
    This was made popular by the widely discussed Web 2.0 . Folksonomy refers to public tagging or
    labelling and extracting/searching/obtaining data from that. You can take del.icio.us for an
    instance wherin you bookmark sites under various categories and that is searchable by others too...
    To be precise, people sort contents into better categories than bots and that help others online to
    get more relevant data. This became popular after these social networking and collob....
  7. Persian (farsi) Languages
    How is Persian(Farsi) for building new scientific words? (0)
    In the name of God In this short article we are going to explain how new words can be built in
    Persian(Farsi), as an 'Indo-European' language (Indo-European languages include languages
    like: Persian(Farsi), Greek, Latin, German, French, English,etc).This article is written based on
    Prof.Hessabi's(the great Iranian scientist that is famous as the father of physics in Iran,peace
    be upon him) discussion on Persian's capabilities vs Arabic. We are going to discuss on the
    structure of Indo-European languages.we want to see how its possible to build many scient....
  8. What Are Acceptable Words In My Forum Signature?
    Some More Questions...regarding sigs & avatars (5)
    Hi, I was wondering about how far can i go with swearing in sigs and avatars? Because my sig i
    usually use has a swear word in it and so does my avatar, also do you allow animated gifs as avatars
    being used? Topic title...topic title...topic title... Nope, it didn't change. So I did. ....
  9. Phpnet
    How do they compare with trap 17? (6)
    I'm looking into signing up for a paid hosting account with someone. I was wondering how you
    think these guys compare with trap17 and their partners computing host.
    http://phpnet.us/paidplans.php vs. http://www.computinghost.com/plans.html ....
  10. Sos...sos...help Me Pls.
    Help to filter bad words (1)
    I should filter bad words and replace them with three dot. I know this is not so difficult to write
    function for such purpose. But function that I wrote doesn't give any result neither necessary
    result nor error. May be you know what's going wrong with my function. Code:
    $abuse_list= array ("abuse","bad"); function filter_bad_words($s) { global
    $abuse_list; foreach ($abuse_list as $k=>$v) {
    eregi_replace("^".$v."$","...",$s); } return $s; } $s="abuse,
    badness, bad wo....
  11. Compare Two Vars And Highlight The Differences
    (7)
    I need a script that compares 2 variables, and looks for a difference between them. It will then
    highlight the words that are different, as shown in the image below: So in the left cell, the
    yellow text is the text that has been removed OR replaced. In the right cell, the green text is the
    text that has replaced some text in the left cell, OR new text. This script will be used to
    compare 2 versions of a document stored in the database, to see the difference between each change
    (all revisions are stored in the database). I know that it's possible with PHP, beca....
  12. Good Advice For Everybody
    Wise words to remember when posting opinions. (4)
    I ran across this in Reader's Digest and thought it worthy of placing up here for
    everybody in the Counceling, Advice, Support forums. QUOTE People will forget
    what you said, people will forget what you did, but people will never forget how you made them feel.
    ...............Maya Angelou ....
  13. Stuck For Words
    (1)
    hi everyone im opening up this new webmaster site, im offering design to people but im not quite
    sure what to add for the packages which are Basic, Moderate and Advanced. I can offer anything
    internet related but i dont know what to put as the features of a perticualr package anyone help?
    Ive done the basic package which sounds like this Dedicated Lifetime Support Professional Design
    Valid HTML,XHTML ect Discounted website hosting Access to client areas im stuck on what to do
    for the Moderate and Advanced packages any ideas on what i could offer?....
  14. Adsense In A Few Words
    another noob asking for help (3)
    ok, i'm sorry if i am just another noob in the adsense ocean but i'm so overhelmed by that
    much info that i get unable to make anything...i always wanted to try it but never got it to a
    result so please help me in a few issues: 1)i have built a website based on Wordpress and i would
    like to get it online. Ok, here at trap17 if possible when i get all those points but my question is
    if it's better to get an url redirection to a co.nr or smth like that as long as it concernes
    seo? is mysite.co.nr better than mysite.trap17.com or it doesn't matter? 2)can....
  15. A Fortunate Accident
    498 Words Sudden Fiction (0)
    This is my first short story -- sudden fiction to be more exact. Tell me what you think. A
    FORTUNATE ACCIDENT JEFF? YOU GAVE MY NUMBER TO JEFF? I was supposed to tell you but that born
    loser texted you before I had the chance. No I mean how do you know Jeff? We’re orgmates in Los
    Banos. We were just sort of talking about you last Friday, we’re a starting a business eh. But
    how does Jeff know that you know me? He doesn’t…I mean he didn’t. Huwhaaat? Ok so I met Mr.
    Llangco the other day and he told me that Jeff met you. Where did Oliver come from? Uhm ....
  16. Google Add Words
    (2)
    HI to everyones!!!!! I did the registration (a long long tima ago) to ADD Words
    di Google. But I can see that google is making a lot of money with my site, and I'm getting
    nothing: from the last statistics, goggle get 25$ in the last month, and I get 0,78$ in
    the same month. I'm not happy!! SOmeone maybe know about a way to encrease Entries
    with the same quantity of code in my site???? Please help!! I beleve that Google is the
    best way to make a good trafic, but not like this!!....
  17. Learn With Pictures, Not Words.
    Computer books based on pictures, not text. FREE! (13)
    A picture is worth a thousand words, it has been said since time immemorial. and for those people
    who are rushing to know and learn varied information, how much easier would it be to understand and
    follow tutorials via pictures instead of reading a bunch of long and convoluted texts? some of you
    are the "more visual" kind of learners who would understand computer-related and web-related topics
    better with pictures, here is a treat for you all. /biggrin.gif" style="vertical-align:middle"
    emoid=":D" border="0" alt="biggrin.gif" /> ALL ebooks below released by INPICS ca....
  18. Internet Jargons And Their Definitions
    Definitions of common words we come across the Internet (3)
    Hope this might be useful QUOTE 0-Day - Latest software releases. 0-Sec - Same as above,
    although the period of time between ripping a game or application and it appearing on a warez site
    is even shorter. Ace File - The first file in a series of compressed archives (the one you double
    click on to decompress all the files at once). Active List - Similar to a mailing list, but uses
    ICQ to send instant messages to subscribers. Alpha - Software receives this label when it is in the
    very early stages of development. Usually full of bugs, so don't touch it with ....
  19. "i Love You"
    Three words we say too often? (11)
    I just read in another thread (about how to say "I love you" in other languages than english) that
    someone in here doesn't say it because it "gets tossed around too often". I think it is not
    being said (or at least thought) often enough. Why? I'll try to explain: At least among many
    people I know, saying "I love you" implies "I want sexual contact with you". If someone says "I love
    you" to gain sexual contact, I agree with those who claim that these three words are being tossed
    around too much - if you want sex without any commitments, why don't you say so?....
  20. Your View And Definition Of Swear Words.
    (10)
    QUOTE ' swear word n. An obscene or blasphemous word '. esp. uttered as an expletive.
    Ok I dont want this thread to turn into a list of examples, but I'd be interested to hear
    your views as to what makes a swear word. What is classed as an indecent word or what offends you,
    in your view? Are some words acceptable in some cases and not in others? Just use upper case first
    initial to signify which word you are referring to. Title modified. ....
  21. Keeping Your Email Address Out Of The Hands Of Spam Artists
    by using escape chars (5)
    Many websites display a contact email address in the format someone(AT)website(DOT)com so that bots
    looking for emails to spam are unable to read the address. It has occurred to me that if you write
    your email in escape chars you will have an equal chance of escaping bot detection and your email
    will appear in the proper format - which is a neater solution and it would allow for the address to
    automatically fire up the user's mail program with the MAILTO: URL type link, an option not
    available using the (AT)(DOT) version. For example, my email address would be writ....
  22. Compare Old From New Sigs
    And comment (5)
    Here's a few of the FIRST sifs I made, I'll show 5 old, and 5 new. 1 2(one with actuall
    brushing) Now here are my most revent sig; 1 2 3 4 5 Sorry if I mispelled anything, I
    got hurt kinda bad today and I can't open my eyes fully. Comnents plase.....
  23. Unknown History Of Words.....interesting
    (0)
    Know some unknown history of words............................ MOPED is the short term for
    'Motorized Pedaling'. POP MUSIC is 'Popular Music' shortened. BUS is the short term
    for 'Omnibus' that means everybody. FORTNIGHT comes from 'Fourteen Nights' (Two
    Weeks). DRAWING ROOM was actually a 'withdrawing room' where people withdrew after Dinner.
    Later the prefix 'with' was dropped. NEWS refers to information from Four directions N, E, W
    and S. AG-MARK, which some products bear, stems from 'Agricultural Marketing'. JOU....
  24. Your First Words
    let us know your cutey words spelled all wrong!! (4)
    My first words at the age of 2 were: Bye and Yes (not very special) But if you do had special first
    words please let us know and reply! /biggrin.gif" style="vertical-align:middle" emoid=":D"
    border="0" alt="biggrin.gif" /> My wrong spelled words: Tifisy - Televisie (Dutch) - Television
    Tangepoesten - Tandenpoetsen (Dutch) - Brush Teeth Locola - Chocolade (Dutch) - Chocolate Did you
    always spelled words wrong way too when you were a little kid? Reply and tell about it, its funny
    really !!! ....
  25. How Can I Search For Song With Only Words
    (7)
    hey how can i search for a song on a internet program. i want a song with the word only like if you
    want the background music only you type e.g. "GetLow kareoke" so what if i want only the word of Get
    Low and no back ground music. How can i search for that?....
  26. Formal Spanish Words
    (14)
    In Spanish, our teacher is always getting on us because we do not conjugate stuff right, because of
    this damned formal stuff, usted and ustedes...why in the world do we have to learn it formally? no
    offfense to anyone who is of a spanish origin, or who is at least trying to learn the lannguage of
    the country they are in.....
  27. Making A Scroll Bar In Flash
    with an image and words on it (4)
    does anybody know how to make a scrollbar for my game page I am making on my site. This whole site
    is completely in flash. I know how to make a scrollbar for just words but not when there is an
    image on there also. Does anybody know?....
  28. Words Of Wisdom
    What Keeps You Going? (26)
    Hi, Ok so right now things are going too well for we. I was just wondering what it is that keeps
    you going in the mornings? What is it that gets you out of bed when the alarm clock goes off, what
    is it that puts you to sleep at night? What words or sayings do you use to help you out in life, and
    how do you motivate yourself to just keep on rolling? Personally, the only thing I can enjoy
    about school is being with my mates. I ain't too kean on it to be honest, but some of my
    favourite quotes to help out with the day are: Train Hard, Fight Easy. You gotta learn ....
  29. Ati Updates Drivers Frequently Compare To Nvidia
    Ati And Nvidia Drivers (9)
    Why does ATI release drivers more often than nVIDIA? I'm a nVIDIA FX5900XT owner and I'm
    very jealous of the ATI people. /biggrin.gif" style="vertical-align:middle" emoid=":D" border="0"
    alt="biggrin.gif" /> Topic title must be specific. Title changed. ....
  30. Spellings Are Not Important.
    Our mind reads words as a whole. (34)
    QUOTE cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg The phaonmneal pweor
    of the hmuan mnid Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer inwaht
    oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist and lsat ltteer be
    in the rghit pclae. The rset can be a taotl mses and you can sitll raed it wouthit a porbelm. Tihs
    is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe. Amzanig
    huh? yaeh and I awlyas thought slpeling was ipmorantt! I think th....

    1. Looking for compare, chars, words

Searching Video's for compare, chars, words
Similar
Client
Corresponden
ce - Keeping
Unflattering
Words Off
The Record
Hello!
(these Words
Need Your
Attention)
Seo = Just
Remember To
Keep Your
Meta Tag To
3 Key Words
No Words? -
Question....
Compare 2
List Of User
Ids From
Different
Tables
Folksonomy -
Folksonomy
--
Folks-Taxono
my or in
other words
"Peoples
classifica
Persian
(farsi)
Languages -
How is
Persian(Fars
i) for
building new
scientific
words?
What Are
Acceptable
Words In My
Forum
Signature? -
Some More
Questions...
regarding
sigs &
avatars
Phpnet - How
do they
compare with
trap 17?
Sos...sos...
help Me Pls.
- Help to
filter bad
words
Compare Two
Vars And
Highlight
The
Differences
Good Advice
For
Everybody -
Wise words
to remember
when posting
opinions.
Stuck For
Words
Adsense In A
Few Words -
another noob
asking for
help
A Fortunate
Accident -
498 Words
Sudden
Fiction
Google Add
Words
Learn With
Pictures,
Not Words. -
Computer
books based
on pictures,
not text.
FREE!
Internet
Jargons And
Their
Definitions
-
Definitions
of common
words we
come across
the Internet
"i Love
You" -
Three words
we say too
often?
Your View
And
Definition
Of Swear
Words.
Keeping Your
Email
Address Out
Of The Hands
Of Spam
Artists - by
using escape
chars
Compare Old
From New
Sigs - And
comment
Unknown
History Of
Words.....in
teresting
Your First
Words - let
us know your
cutey words
spelled all
wrong!&#
33;
How Can I
Search For
Song With
Only Words
Formal
Spanish
Words
Making A
Scroll Bar
In Flash -
with an
image and
words on it
Words Of
Wisdom -
What Keeps
You Going?
Ati Updates
Drivers
Frequently
Compare To
Nvidia - Ati
And Nvidia
Drivers
Spellings
Are Not
Important. -
Our mind
reads words
as a whole.
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