Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Counting Char's Within Strings

Thread view: 
BlackJackal - 31 Jan 2007 21:27 GMT
Alright a couple of stupid questions here about strings and Chars.
First off here is my Code

   public class CountVowels
  {
      public static void main(String[] args)
     {
        int vowel = 0;
        int i;
        char pos;
        String String1 = "Event Handlers is dedicated to making your
event a most memorable one.";
        int length = String1.length();
        for(i = 0; i < length - 1 ; i++);
        {
           pos = String1.charAt(i);
          if (pos == 'A' || pos == 'a' || pos == 'E' || pos == 'e' ||
pos == 'I' || pos == 'i' || pos == 'O' || pos == 'o' || pos == 'U' ||
pos == 'u') {
              vowel += 1;
           }
        }
        System.out.println("There are " + vowel + " vowels in this
String");
     }
  }

First question is why does String1.length() return 70 when I only
count 69?  The other question is why does this code always produce 0
vowels?

Thanks in advance I am just a little stumped.
Knute Johnson - 31 Jan 2007 21:51 GMT
> Alright a couple of stupid questions here about strings and Chars.
> First off here is my Code
[quoted text clipped - 25 lines]
> First question is why does String1.length() return 70 when I only
> count 69?

Because you can't count.

> The other question is why does this code always produce 0
> vowels?

The problem is in this line!

>          for(i = 0; i < length - 1 ; i++);

Signature

Knute Johnson
email s/nospam/knute/

Daniel Pitts - 01 Feb 2007 01:26 GMT
On Jan 31, 1:51 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > Alright a couple of stupid questions here about strings and Chars.
> > First off here is my Code
[quoted text clipped - 39 lines]
> Knute Johnson
> email s/nospam/knute/

You should be nice and tell him why that line is wrong.
Since you have a ; after the ), it tells that for loop to do *nothing*
for all i between [0, length-1)
it really should be
for (i = 0; i < length; ++i) {
// do vowal county bit.
}
Knute Johnson - 01 Feb 2007 02:24 GMT
> On Jan 31, 1:51 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> wrote:
[quoted text clipped - 45 lines]
> // do vowal county bit.
> }

I guess it is difficult to express 'ribbing' through the post.

As to the ;, I was trying to be helpful but not give the fellow the
direct answer as we have all done this at one time or another.  Finding
being its own great reward, I thought he would see what I posted and
slap his head and say "Oh that was dumb", as I have on several
occasions.  In fact it took me a couple minutes to find it this time.

Signature

Knute Johnson
email s/nospam/knute/

Luc The Perverse - 01 Feb 2007 04:22 GMT
> I guess it is difficult to express 'ribbing' through the post.
>
[quoted text clipped - 3 lines]
> and say "Oh that was dumb", as I have on several occasions.  In fact it
> took me a couple minutes to find it this time.

I have seen classes where ";" is simply described as the end of a
statement - which I suppose is not specifically "wrong" but to someone who
had misinterpreted it early on could cause continued frustration.

It would be good for any instructor to head this one off by not only showing
how the "mistake" could be made, but by showing how in certain circumstances
doing nothing inside a loop would make sense.

--
LTP

:)
Proton Projects - Moin - 01 Feb 2007 04:26 GMT
Hi Knute,

How this code will help to find vowels in a particular string

if ("AaEeIiOoUu".indexOf(pos) >= 0) {
                 vowel += 1;
             }

"AaEeIiOoUu" is a complete string....

Or else u r asking to do like this

"A".indexOf(pos)>=0
"E".indexOf(pos)>=0
"I".indexOf(pos)>=0
"O".indexOf(pos)>=0
"U".indexOf(pos)>=0

Thanks,
Moin

On Feb 1, 7:24 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
> > On Jan 31, 1:51 pm, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
> > wrote:
[quoted text clipped - 58 lines]
> Knute Johnson
> email s/nospam/knute/
Chris Dollin - 01 Feb 2007 08:34 GMT
> Hi Knute,
>
[quoted text clipped - 13 lines]
> "O".indexOf(pos)>=0
> "U".indexOf(pos)>=0

No, he meant exactly what he wrote: the expression

   "AaEeIiOoUu".indexOf(pos) >= 0

is true iff the character `pos` is one of AaEeIiOoUu.

PS Is `y` a vowel?

Signature

Chris "electrick hedgehog" Dollin
"We live for the One, you die for the One." Unsaid /Babylon 5/.

Jeff Higgins - 01 Feb 2007 14:55 GMT
> PS Is `y` a vowel?

Wow! Thanks for asking. That was great fun.
[http://en.wikipedia.org/wiki/Vowel#Written_vowels]

--
Jeff Higgins
Lew - 01 Feb 2007 18:30 GMT
Chris Dollin wrote:
>> PS Is `y` a vowel?

> Wow! Thanks for asking. That was great fun.
> [http://en.wikipedia.org/wiki/Vowel#Written_vowels]

This came up in a recent thread hereabouts. Bear in mind that Java is an i18n
environment, and not all locales have "vowels".

- Lew
Lew - 01 Feb 2007 18:31 GMT
> not all locales have "vowels".

I mean, not all have the same "vowels".

- Lew
Oliver Wong - 01 Feb 2007 21:03 GMT
>> not all locales have "vowels".
>
> I mean, not all have the same "vowels".

   I am now tempted to discover or invent a language which does not have
any vowels.

   - Oliver
Jeff Higgins - 01 Feb 2007 22:32 GMT
>    I am now tempted to discover or invent a language which does not have
> any vowels.
>
>    - Oliver
Look no further than some posts in this group. (cn y rd ths?),
Luc The Perverse - 01 Feb 2007 23:03 GMT
>>> not all locales have "vowels".
>>
>> I mean, not all have the same "vowels".
>
>    I am now tempted to discover or invent a language which does not have
> any vowels.

That would certainly sound interesting.

Of course it depends on what you mean.  For a language to have no vowel
sounds would make it very . .  unfluid.

But for instance Hebrew only records consonant sounds

--
LTP

:)
Chris Uppal - 02 Feb 2007 12:50 GMT
> But for instance Hebrew only records consonant sounds

Arabic too, as I understand it.

   -- chris
Tim Slattery - 02 Feb 2007 17:25 GMT
>> But for instance Hebrew only records consonant sounds
>
>Arabic too, as I understand it.

My understanding is that written versions of both languages represent
vowel sounds as diacritical marks over the letters.

The oldest versions of the old testament are in Hebrew. They have no
diacritics, therefore no vowels, and no spaces between words!

--  
Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt
Luc The Perverse - 02 Feb 2007 21:50 GMT
>>> But for instance Hebrew only records consonant sounds
>>
[quoted text clipped - 5 lines]
> The oldest versions of the old testament are in Hebrew. They have no
> diacritics, therefore no vowels, and no spaces between words!

No spaces?  Are you sure you aren't think of Latin?

I have seen Hebrew text . . and these has always been . . spaces

--
LTP

:)
Lew - 02 Feb 2007 23:24 GMT
> I have seen Hebrew text . . and these has always been . . spaces

Captain's . Log .  .  Stardate . Fifty-Three -  - - Forty- . .   . two point .
  . . five: ...

- Lew
Oliver Wong - 02 Feb 2007 16:06 GMT
>>>> not all locales have "vowels".
>>>
[quoted text clipped - 9 lines]
>
> But for instance Hebrew only records consonant sounds

   I'm working with the definition of vowel which says that it is a sound.
That is, letters in themselves cannot be classified as being vowels or not,
but the pronunciations of letters can be classified as being vowels or not
(similarly, invisible things like "love" or "honor" cannot be classified as
being red or not, but visible things like "my car" or "clouds" can be
classified as being red or not).

   I don't know about Hebrew, but I'd imagine it *does* have vowel sounds,
even if you do not explicitly write them. Similarly, as another poster
noted, I wouldn't count "cn y rd ths?" as being a vowel-less language. There
are vowels -- they are simply implied.

   I asked a linguist friend of mine about it, and she said that while it's
logically possible for such a language to exist (e.g. if the language were
composed only of the nasal "n" sound, and timing to communicate, sounding
like a series of grunts of varying duration), it would be extremely unlikely
for such a language to naturally evolve into existence. So any such language
would probably be artificially invented.

   - Oliver
Patricia Shanahan - 02 Feb 2007 16:18 GMT
>>>>>not all locales have "vowels".
>>>>
[quoted text clipped - 16 lines]
> being red or not, but visible things like "my car" or "clouds" can be
> classified as being red or not).

That may well be a very logical definition of vowel, but it is probably
not the appropriate definition in the context of an attempt to count
characters in a string, the original point of this thread.

Patricia
Luc The Perverse - 02 Feb 2007 16:38 GMT
>    I don't know about Hebrew, but I'd imagine it *does* have vowel sounds,
> even if you do not explicitly write them. Similarly, as another poster
> noted, I wouldn't count "cn y rd ths?" as being a vowel-less language.
> There are vowels -- they are simply implied.

That is what I meant - I was looking for clarification - was he looking for
a language with no vowel sounds or a written format with no letters which
represent sounds?

--
LTP

:)
John W. Kennedy - 04 Feb 2007 20:25 GMT
>     I don't know about Hebrew, but I'd imagine it *does* have vowel sounds,
> even if you do not explicitly write them.

In modern Hebrew, a couple of now-unused consonants are employed as
vowels, and other vowels are indicated by diacritics (as in Tengwar).

Ancient Hebrew, in common with other West Semitic languages, did not
write vowels because large amounts of West Semitic grammar are handled
by ablaut (like English "sing", "sang", "sung"), so that it is possible
to get a good idea of the meaning of any sentence when it is written
with only the consonants. This was, in fact, of crucial importance in
the evolution of the alphabet. The West Semitic languages received
writing in the form of a syllabary, like Cherokee or Japanese kana, in
which every symbol represented a syllable: ba, be, bi, bo, bu, ga, ge,
gi, go, gu, da, de, di, do, du, and so forth. Because of not needing to
write the vowels, they were able to cut down the number of characters to
a mere twenty-odd, which vastly reduced the amount of memorization
needed to learn to read and write. When the Greeks picked up the West
Semitic alphabet from the Phoenicians, they found that they needed to
write the vowels for their language, so picked a few consonants that
they didn't need to use as vowels instead, created variations on a few
more letters, and then they were ready to go. The Latin and Cyrillic
alphabets, and many more, derive from the Greek.

See the really cool animations at
<URL:http://www.wam.umd.edu/~rfradkin/alphapage.html>.

Signature

John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
  -- Charles Williams.  "Taliessin through Logres: Prelude"

Jeff Higgins - 01 Feb 2007 22:25 GMT
> Chris Dollin wrote:
>>> PS Is `y` a vowel?
[quoted text clipped - 4 lines]
> This came up in a recent thread hereabouts. Bear in mind that Java is an
> i18n environment, and not all locales have "vowels".

Well, it's still been an enjoyable few minutes browsing links from the
search string "vowel". :-)
Now here are some people who have seriously considered vowels. R-colored
vowels!

PS Just a thought. How many vowels in "thought".
> - Lew
Alex Hunsley - 31 Jan 2007 22:04 GMT
> Alright a couple of stupid questions here about strings and Chars.
> First off here is my Code
[quoted text clipped - 28 lines]
>
> Thanks in advance I am just a little stumped.

Please in future post code that is directly compilable. Your long
strings (>80 chars or so) are broken in your post, and have to be
manually fixed in order to compile. In future, this style helps:

  String longString = "This is a really "
    + "long string I am making so "
     + "I will split it up like this";

As for your no vowels counted problem - it's a subtle one, this: you've
put a ';' after your for statement. This means what you think is the
body of your for is actually a separate statement that run after the for
loop. The for loop itself is running from 0 to 69 and doing nothing (;)
each time. Remove the ';' at end of the for line and it works.

You only count 69 chars? You sure? Count again... did you include the
full stop at the end? Did you actually count them, or are you being
mislead by the fact the for loop goes up to 'length - 1'?

Finally, that horrid big 'if' check for the vowels can be better written
this way:

             if ("AaEeIiOoUu".indexOf(pos) >= 0) {
                 vowel += 1;
             }

If you make sure pos contains a lower-case char, you could even just do
"aeiou".indexOf...

lex
voorth - 01 Feb 2007 15:44 GMT
> Alright a couple of stupid questions here about strings and Chars.

The simplest way, of course, uses regular expressions:

int countVowels(String input) {
 return input.replaceAll("[^aeiouyAEIOUY]", "").size();
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.