Results 1 to 9 of 9
- 01-07-2012, 06:42 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Counting Vowels, getting an error
I am trying to create a program that counts the number of vowels in an array. I keep getting the following error:
Vowels.java:12: error: cannot find symbol
c = name.charAt(i);
^
symbol: method charAt(int)
location: variable name of type String[]
1 error
Here is my code:
import java.util.*;
import javax.swing.*;
public class Vowels
{
public static void main(String[] args)
{
String[] name = {"Mariel", "Yadrianna", "Zeus", "Gabriel", "Luz"};
char c;
int i;
int count = 0;
for(i = 0; i < name.length; i++)
{
c = name.charAt(i);
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u')
{
count++;
}
}
}
}
I know I still have to print or display the actual number of vowels.
- 01-07-2012, 06:47 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Counting Vowels, getting an error
name = string array.an array has no method charAt.
you have to iterate over each element in the array(each name) and then you have to iterate over each character of this string (i think that was what you want - there are of course other solutions)
pseudo:
for(0...array.length)
name = array[i];
for(0 ... name.length)
character = name.charAt(j)
.....
- 01-07-2012, 06:53 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
- 01-07-2012, 06:56 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Counting Vowels, getting an error
Because you are trying to invoke the charAt method on an array object (String[] name) and not from a string!
name[0].charAt would be valid!
- 01-07-2012, 06:58 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
- 01-07-2012, 07:05 PM #6
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Re: Counting Vowels, getting an error
Thats what i said in my first post :D
But there are other solutions, like
:DJava Code:for (String string : name) { System.out.println("Vowel count of "+string+" is "+string.replaceAll("[^aeiou]", "").length()); }
- 01-07-2012, 07:08 PM #7
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: Counting Vowels, getting an error
Sorry for my noobinesh. I will keep working on this and post the final product once I figure it out.
- 01-07-2012, 07:46 PM #8
Member
- Join Date
- Nov 2011
- Posts
- 41
- Rep Power
- 0
Re: Counting Vowels, getting an error
Thanks for the help. Here is the final working code:
import java.util.*;
import javax.swing.*;
public class Vowels
{
public static void main(String[] args)
{
String[] name = {"Mariel", "Yadrianna", "Zeus", "Gabriel", "Luz"};
int i;
int count = 0;
for(String string: name)
{
System.out.println("Vowel count of "+string+" is "+string.replaceAll("[^aeiou]", "").length());
}
for(i = 0; i < name.length; i++)
{
char c = name[0].charAt(i);
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u' ||
c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
{
count++;
}
}
}
}
- 01-07-2012, 07:50 PM #9
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 780
- Rep Power
- 4
Similar Threads
-
Counting exponent with long variables and Arrays, still error .. help anyone?
By breakdance77 in forum New To JavaReplies: 4Last Post: 08-12-2011, 05:47 PM -
counting the vowels in a sentence
By huntrguy102 in forum New To JavaReplies: 3Last Post: 11-08-2010, 06:31 AM -
Replacing Vowels in a word.
By mklprasad in forum Advanced JavaReplies: 1Last Post: 10-05-2009, 12:31 PM -
Code counting error
By Adde1986 in forum New To JavaReplies: 2Last Post: 03-18-2009, 01:02 AM -
Counting Vowels and Constonants
By MattN in forum New To JavaReplies: 3Last Post: 11-20-2007, 05:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks