• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

vowels method

 
Jade Davidson
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a method that takes a String and returns a new String with only the vowels (or only the consonants). This is what I have so far. What I want is to go through the String with a loop and take out the vowels. But I don't think I am doing it right.

import java.util.*;
import java.lang.*;
public class Name {
public static void main(String[] args) {
String name = "Brian Candido";
String s = name.toLowerCase();
int cows ;
for (int i = 0; i < s.length(); i++){
if (s.charAt(i) == ' ')
continue;
else
cows = ((s.charAt(i) - 'a') % 9) + 1;
System.out.println(cows);
System.out.print(return_vowel(s));
}
}
public static String return_vowel (String s) {
String n = s;
for (int i = 0; i < n.length(); i++){
if (n.charAt(i) == 'a' | | n.charAt(i) == 'e' | | n.charAt(i) == 'i'
| | n.charAt(i) == 'o' | | n.charAt(i) == 'u' | | n.charAt(i) == 'y')
String vowel = (n.charAt(i));
return vowel;
}
}
}
 
Rene Marot
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
public static String return_vowel (String s) {
String vowel //Move definition here
String n = s;
for (int i = 0; i < n.length(); i++){
if (n.charAt(i) == 'a' | | n.charAt(i) == 'e' | | n.charAt(i) == 'i'
| | n.charAt(i) == 'o' | | n.charAt(i) == 'u' | | n.charAt(i) == 'y'){
vowel = vowel+(n.charAt(i));//Cacnatenate current char with previous result
}
}
return vowel;//Return result when you have finish with the loop
}
I did test it, but your code should near that.
And for simpler readin use indentention for "if()" and "for()"
statements.
Hope it helps.
Bye
 
Junilu Lacar
Bartender
Pie
Posts: 7465
50
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an alternative way to test for a vowel:



that way, you can write



This is much more expressive and conveys the intent of the code much better. You might want to be more descriptive with your variable names too: "cows" doesn't tell me a whole lot about what it actually represents.
Last tip: Please try to format your code to make it easier to read. Edit this message to see how to use the UBB codes and HTML to format sections of code.
Junilu
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite sure what you are doing with cows, but I would think that you would want to put the
System.out.print( return_vowel( s ));
line outside of the for loop.

Also, doing what Rene suggested should get you all the vowels in the String.

<pre> public static String getVowels( String s )
{
String vowels = "" ;

for ( int i = 0; i < s.length(); i++ )
{
char character = s.charAt( i );
boolean isVowel = ( character == 'a' ) | | ( character == 'e' ) | | ( character == 'i' )
| | ( character == 'o' ) | | ( character == 'u' ) | | ( character == 'y' ) ;

if ( isVowel )
{
vowels += character ;
}
}
return vowels ;
}</pre>

[This message has been edited by Marilyn deQueiroz (edited November 20, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic