Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30
Like Tree1Likes

Thread: If Statement Modification (Using String Methods)

  1. #21
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    Look at what you are printing. There are two indexes: one in the word, one in the vowels.

    print out the value of i also.

  2. #22
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: If Statement Modification (Using String Methods)

    Java Code:
    String word = "Daniel";        String vowels = "AaEeIiOoUu";
    
    
    
    
            
            for(int i=0; i<vowels.length(); i++){//loop through all the letters
                
    
    
                if(word.indexOf(vowels.charAt(i))!=-1){
    
    
                    System.out.println(word.indexOf(vowels.charAt(i)));
                    i=word.length();
                }
    
    
    
    
    
    
            
    }
    Ok so I got the right code in my test. However, when I try to translate 'beams' instead of finding index of 1, it displays 2. Why is it skipping the e, it should be checking it. It seems to be looking for A's first and then E's. How can I make it so it shows the earliest one in word rather than the earliest vowel the computer checks?
    Last edited by danthegreat; 09-11-2011 at 02:53 PM.

  3. #23
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    What does the value 2 represent?

    What are you doing with the value of i: i=word.length()
    Why are you setting it to this value?
    What value of i is tested in the for statement?
    Last edited by Norm; 09-11-2011 at 02:56 PM.

  4. #24
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: If Statement Modification (Using String Methods)

    2 represents the 'a' found. I realize that the computer is searching by the order of the string 'vowels'. If it finds an A anywhere in the string, then it will stop the loop without searching for earlier vowels.

    How can I make it so the computer checks the earliest vowel in string 'word' rather than checking the earliest vowel in string 'vowels'?

  5. #25
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    2 represents the 'a' found.
    That is not clear since 'a' is in both the word String and the vowels String.
    A clearer way to say this is that 2 is the index into one of these String: word or vowels

  6. #26
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    What are you doing with the value of i: i=word.length()
    Why are you setting it to this value?
    What value of i is tested in the for statement?

  7. #27
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: If Statement Modification (Using String Methods)

    sorry that was a typo its vowels.length()

  8. #28
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    You need to define what you are trying to do. You seem to be going this way and that way.
    Do you want the index of the first vowel in the String word? or what?
    What variable has the index of the letters in the String word?

    Do you want to know if the String word has a vowel?
    The return true if so and false if not.

  9. #29
    danthegreat is offline Member
    Join Date
    Sep 2011
    Location
    Washington DC
    Posts
    51
    Rep Power
    0

    Default Re: If Statement Modification (Using String Methods)

    I figured it out: (This is my final hasAVowel() Function) -

    Java Code:
    public static int hasAVowel(String word){//detects if the word has a vowel and returns location
    
        
            int index=0, pos=999;
            String vowels = "AaEeIiOoUu";
    
    
                    
            for(int i=0; i<vowels.length(); i++){//loop through all the letters
                
    
    
                if(word.indexOf(vowels.charAt(i))!=-1){
                    
                    if(pos>=word.indexOf(vowels.charAt(i))){
                        pos=word.indexOf(vowels.charAt(i));
                    }
            
    
    
                }
            
    
    
            }    
            
            index=pos;
    
    
            return index;
    }
    pos is a new variable that makes sure the earliest vowel is saved. For example: "Beams"

    the computer finds 'a' at pos1 = 2. However, there is an 'e' in front of it which is the FIRST OCCURRENCE. Then, the computer checks for the e's and finds that 'e' is at pos1 = 1. Then the if statement checks if pos1>pos2, save the earliest occurrence of a vowel. It then saves pos = 1 because it is earlier than pos =2.
    Last edited by danthegreat; 09-11-2011 at 03:13 PM.

  10. #30
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,457
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    What if you took a letter in word one at a time and tested if it was in the vowels String?
    If the letter is in vowels then the index into word for the letter found in vowels would be what you returned.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Calling Methods in switch Statement
    By borth92 in forum New To Java
    Replies: 7
    Last Post: 12-09-2010, 09:14 AM
  2. Using the return statement to display a string
    By Hoverboy in forum New To Java
    Replies: 6
    Last Post: 11-17-2010, 09:22 AM
  3. If Else Statement using String
    By j@v@ in forum New To Java
    Replies: 2
    Last Post: 10-08-2010, 04:29 PM
  4. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  5. Sql string with callable statement..
    By nathan in forum JDBC
    Replies: 1
    Last Post: 09-24-2008, 01:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •