The Artima Developer Community
Sponsored Link

Java Answers Forum
finding the first vowel...

3 replies on 1 page. Most recent reply: Nov 28, 2002 10:25 AM by Matthew Pica

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Matthew Pica

Posts: 5
Nickname: mrpica05
Registered: Nov, 2002

finding the first vowel... Posted: Nov 27, 2002 9:21 PM
Reply to this message Reply
Advertisement
I am working on a method for a project(one that translates into pig latin) that finds the first vowel in a word string. I have started the method, and am just experimenting with the vowel 'a' to keep it simple. It finds the position in the string and assigns that value to an int variable called vowelPosition. this is set to negative one, so if the word doesnt have any vowels it should stay as negative one, HOWEVER when i used "bat" and "apple" it stayed as negative one. It should have returned 1 and zero respectively. Here is my coding, hopefully someone can shead some light on my error.


public static int findFirstVowelPosition(String word)
{
int vowelPosition = -1;
word = word.trim();

for(int i=1; i<=word.length(); i++)
{
if(word.substring(0,i).equalsIgnoreCase("a"))
vowelPosition = i;
}

return vowelPosition;
}

Thanks in advance,

Matthew


sateesh

Posts: 1
Nickname: bsateesh
Registered: Nov, 2002

Re: finding the first vowel... Posted: Nov 27, 2002 11:18 PM
Reply to this message Reply
see the following code

public class Test
{
public static void main(String args[])
{
int vowelPosition = -1;
String word = args[0];
word = word.trim();
String word1 = word;
for(int i=0; i<word.length(); i++)
{
if(word1.substring(i,i+1).equalsIgnoreCase("a"))
{
vowelPosition = i;
break;
}
word = word1;

}
System.out.println("position "+vowelPosition);
}
}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: finding the first vowel... Posted: Nov 28, 2002 7:59 AM
Reply to this message Reply
    public static int findFirstVowelPosition(String word)
    {
         int vowelPosition = -1;
        word = word.trim();
        
        for(int i=1; i <=word.length(); i++)  
         {      
            if ((word.charAt(i-1) == 'a') ||(word.charAt(i-1) == 'A')
             ||(word.charAt(i-1) == 'e') ||(word.charAt(i-1) == 'E')
             ||(word.charAt(i-1) == 'i') ||(word.charAt(i-1) == 'I')
             ||(word.charAt(i-1) == 'o') ||(word.charAt(i-1) == 'O')
             ||(word.charAt(i-1) == 'u') ||(word.charAt(i-1) == 'U')
             ||(word.charAt(i-1) == 'y') ||(word.charAt(i-1) == 'Y')){
               vowelPosition = i;
                return vowelPosition;
             }
         } 
         return vowelPosition;
    }

Matthew Pica

Posts: 5
Nickname: mrpica05
Registered: Nov, 2002

Re: finding the first vowel... Posted: Nov 28, 2002 10:25 AM
Reply to this message Reply
Thanks guys for the quick responses. I appreciate the helping hand...ill go through and try to use your suggestions to help solve it. thanks again :^)

-Matthew

Flat View: This topic has 3 replies on 1 page
Topic: Java .Net Interoperability Previous Topic   Next Topic Topic: To start multiple JVM's

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use