• Log in or sign up for an account here: https://www.sythe.org/login

Help

wtp


Banned
Joined
Nov 25, 2005
Sythe Gold
0
Messages
2,455
Location
Perth Amboy, NJ...Sorry no lame joke
IMG_20110422_205424.jpg


I'm trying to do the second problem there. What would I have to do to make it check if it's a vowel or not?
 
Write a method that takes an int i as a parameter and run it through a loop that iterates through the string at index i. Then use an if statement to see if the String at index i equals a vowel. If so return true if not return false.
 
The String class in Java has the method - charAt(int i)

You can use that in the if statement to see if it equals a vowel. If so, increment the vowel counter.

Something like this;

if(input.charAt(i) == 'a' || input.charAt(i) == 'e' ... i, o, u)
{
increment the vowel counter...
increment the index variable...
}
 
Thanks guys for the help. I really appreciate it. I'm almost there, but I'm not sure how to make it loop correctly. I'm not getting any errors.

Code:
import java.util.Scanner;

public class two
{
	static double vowel;
	static String word;
	static String word2;
	static int i = 0;
	static int count = -1;

	public static void main(String[] args)
	{
		Scanner keyboard = new Scanner(System.in);

		System.out.println("Enter charcter(s)");
			word = keyboard.nextLine();

		isVowel(word);

		System.out.println("There are/is " + vowel +
						    " vowel(s) in the entry: "
						    + word);
	}

	public static void isVowel(String word2)
	{
		while(count < i)
		{

		if(word2.charAt(i) == 'a' || word2.charAt(i) == 'e' ||
		   word2.charAt(i) == 'i' || word2.charAt(i) == 'o' ||
		   word2.charAt(i) == 'u')
		   	{
		   		vowel++;
		   		i++;
		   		count++;
		   	}
		else
				i++;
				count++;
		}
	}
}

Output:

Enter charcter(s)
aio
There are/is 1.0 vowel(s) in the entry: aio
 
You should do

Code:
while(count < word2.length()) {


}

That will iterate through the entire string instead of just the first letter.
 
It's giving me an error when I do that:

cannot find symbol
symbol : variable length
location: class java.lang.String
while(count < word2.length)
^

Oh sorry its .length(); not just .length;
 
duelarena.gg
Back
Top