Counting Char's Within Strings

Discussion in 'Java' started by BlackJackal, Jan 31, 2007.

  1. BlackJackal

    BlackJackal Guest

    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.
    BlackJackal, Jan 31, 2007
    #1
    1. Advertising

  2. BlackJackal wrote:
    > 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?


    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++);


    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Jan 31, 2007
    #2
    1. Advertising

  3. BlackJackal

    Alex Hunsley Guest

    BlackJackal wrote:
    > 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.


    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
    Alex Hunsley, Jan 31, 2007
    #3
  4. BlackJackal

    Daniel Pitts Guest

    On Jan 31, 1:51 pm, Knute Johnson <>
    wrote:
    > BlackJackal wrote:
    > > 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?

    >
    > 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++);

    >
    > --
    >
    > 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.
    }
    Daniel Pitts, Feb 1, 2007
    #4
  5. Daniel Pitts wrote:
    > On Jan 31, 1:51 pm, Knute Johnson <>
    > wrote:
    >> BlackJackal wrote:
    >>> 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?

    >> 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++);

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


    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.

    --

    Knute Johnson
    email s/nospam/knute/
    Knute Johnson, Feb 1, 2007
    #5
  6. "Knute Johnson" <> wrote in message
    news:4bcwh.154443$...
    > 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.


    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

    :)
    Luc The Perverse, Feb 1, 2007
    #6
  7. 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 <>
    wrote:
    > Daniel Pitts wrote:
    > > On Jan 31, 1:51 pm, Knute Johnson <>
    > > wrote:
    > >> BlackJackal wrote:
    > >>> 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?
    > >> 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++);

    >
    > >> --

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

    >
    > 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.
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
    Proton Projects - Moin, Feb 1, 2007
    #7
  8. BlackJackal

    Chris Dollin Guest

    Proton Projects - Moin wrote:

    > 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


    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?

    --
    Chris "electrick hedgehog" Dollin
    "We live for the One, you die for the One." Unsaid /Babylon 5/.
    Chris Dollin, Feb 1, 2007
    #8
  9. BlackJackal

    Jeff Higgins Guest

    Jeff Higgins, Feb 1, 2007
    #9
  10. BlackJackal

    voorth Guest

    On Jan 31, 10:27 pm, "BlackJackal" <> wrote:
    > 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();
    }
    voorth, Feb 1, 2007
    #10
  11. BlackJackal

    Lew Guest

    Chris Dollin wrote:
    >> PS Is `y` a vowel?


    Jeff Higgins wrote:
    > 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, Feb 1, 2007
    #11
  12. BlackJackal

    Lew Guest

    Lew wrote:
    > not all locales have "vowels".


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

    - Lew
    Lew, Feb 1, 2007
    #12
  13. BlackJackal

    Oliver Wong Guest

    "Lew" <> wrote in message
    news:...
    > Lew wrote:
    >> 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
    Oliver Wong, Feb 1, 2007
    #13
  14. BlackJackal

    Jeff Higgins Guest

    "Lew" <> wrote in message
    news:...
    > Chris Dollin wrote:
    >>> PS Is `y` a vowel?

    >
    > Jeff Higgins wrote:
    >> 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".
    >

    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
    Jeff Higgins, Feb 1, 2007
    #14
  15. BlackJackal

    Jeff Higgins Guest

    Oliver Wong wrote:
    > 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?),
    Jeff Higgins, Feb 1, 2007
    #15
  16. "Oliver Wong" <> wrote in message
    news:AAswh.59$...
    > "Lew" <> wrote in message
    > news:...
    >> Lew wrote:
    >>> 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

    :)
    Luc The Perverse, Feb 1, 2007
    #16
  17. BlackJackal

    Chris Uppal Guest

    Luc The Perverse wrote:

    > But for instance Hebrew only records consonant sounds


    Arabic too, as I understand it.

    -- chris
    Chris Uppal, Feb 2, 2007
    #17
  18. BlackJackal

    Oliver Wong Guest

    "Luc The Perverse" <> wrote in message
    news:...
    > "Oliver Wong" <> wrote in message
    > news:AAswh.59$...
    >> "Lew" <> wrote in message
    >> news:...
    >>> Lew wrote:
    >>>> 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


    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
    Oliver Wong, Feb 2, 2007
    #18
  19. Oliver Wong wrote:
    > "Luc The Perverse" <> wrote in message
    > news:...
    >
    >>"Oliver Wong" <> wrote in message
    >>news:AAswh.59$...
    >>
    >>>"Lew" <> wrote in message
    >>>news:...
    >>>
    >>>>Lew wrote:
    >>>>
    >>>>>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

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


    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
    Patricia Shanahan, Feb 2, 2007
    #19
  20. "Oliver Wong" <> wrote in message
    news:KjJwh.32487$...
    > 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

    :)
    Luc The Perverse, Feb 2, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    537
  2. wwj
    Replies:
    24
    Views:
    2,477
    Mike Wahler
    Nov 7, 2003
  3. ranjmis
    Replies:
    14
    Views:
    513
    Ben C
    Mar 12, 2006
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,007
    Ian Collins
    May 9, 2006
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    186
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page