Counting Char's Within Strings

B

BlackJackal

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

Knute Johnson

BlackJackal said:
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!
 
A

Alex Hunsley

BlackJackal said:
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
 
D

Daniel Pitts

BlackJackal said:
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++);


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

Knute Johnson

Daniel said:
BlackJackal said:
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++);


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

Luc The Perverse

Knute Johnson said:
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.
 
P

Proton Projects - Moin

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

Daniel said:
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++);
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.
 
C

Chris Dollin

Proton said:
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?
 
V

voorth

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();
}
 
J

Jeff Higgins

Lew said:
Jeff said:
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".
 
J

Jeff Higgins

Oliver said:
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?),
 
L

Luc The Perverse

Oliver Wong said:
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
 
O

Oliver Wong

Luc The Perverse said:
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
 
P

Patricia Shanahan

Oliver said:
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
 
L

Luc The Perverse

Oliver Wong said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top