Question:
Please help me with this Java exercise?
Rayan
2013-01-12 08:47:11 UTC
I am a beginner at learning Java. i need help with this exercise. IF you could help me it would be great.
The exercise is:
Find out how many vowels has one String.
This is my code:
import java.util.Scanner;
public class Sdf {


public static void main(String[] args) {
// TODO Auto-generated method stub

char array[]=new char[50];
int j=0,k,i;

Scanner input = new Scanner(System.in);
niza = input.next().toCharArray();

for(i=0;i<100;i++)
{
if (array[i] == 'a' || array[i] == 'e' || array[i] == 'i' || array[i] == 'o' || array[i] == 'u')
j++;
}
System.out.println("The word has "+ j + " vowels.");
}

}
Three answers:
Kaydell
2013-01-13 22:28:12 UTC
Yes, as somebody else mentioned, you should use a String rather than an array to get input from the user because you want to keep track of how long the String is that the user entered. When you convert it to a char array like you did, then you don't really know how long the String is. Well, I guess that you *could* do it by looking for a null character, but I'll show you how to do it with a String.



Here's my solution:

http://ideone.com/Cl7MPP
Jesse
2013-01-12 09:09:01 UTC
Well, there are several errors here so let's get started. First, you cannot simply declare something as an array variable. The correct form is:



char[] niza = new char[50];



Unfortunately, however, this does not sound like what the question asked. You want to use a string, not an array, so the code should probaby look like this:



System.out.println("Please enter a string: ");

String s = input.nextLine();



The next problem I see is the for loop. Instead of i < 100 you should have i < s.length() which performs the operation on the exact number of characters in the string instead of some arbitrary.



Finally, when comparing letters in Java it is always better to first convert them all to lowercase and then their ASCII numerical value:



String s1 = "";



for (int i = 0 ; i < s.length(); i++){

// Convert the current character to it's ASCII decimal value.

int val = (int)s.charAt(i);

if (val >= 97 && val <= 122){

s1 += (char)(val-32);

}

else

s1 += s.charAt(i);

}



Finally, we can convert to ASCII and check the number of vowels:



for (int i=0 ; i < s.length() ; i++){

int val = (int)s1.charAt(i);

if (val == 96 | val == 101 | val == 105 | val == 111 | val == 117){

j++;}

}





That should work, let me know!
brilliant_moves
2013-01-12 10:49:10 UTC
I think this is what you're after:



import java.util.Scanner;

public class Sdf {



public static void main(String[] args) {

// TODO Auto-generated method stub



char chArray[];

int numVowels=0, i;



Scanner input = new Scanner(System.in);

System.out.print("Type a word: ");

chArray = input.next().toCharArray();



for(i=0; i
if (chArray[i] == 'a' || chArray[i] == 'e' || chArray[i] == 'i'

|| chArray[i] == 'o' || chArray[i] == 'u')

numVowels++;

}



System.out.println("The word has "+ numVowels + " vowels.");

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...