File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes vowels method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "vowels method" Watch "vowels method" New topic
Author

vowels method

Jade Davidson
Ranch Hand

Joined: Oct 12, 2001
Posts: 64
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

Joined: Jun 27, 2001
Posts: 9
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

Joined: Feb 26, 2001
Posts: 4419
    
    5

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


Junilu - [How to Ask Questions] [How to Answer Questions]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
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).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: vowels method
 
Similar Threads
String Array
application to applet
Sorting Array of Strings
ArrayList Problem
translate to pig latin!? completely lost....