Page 1 of 2 12 LastLast
Results 1 to 20 of 30
Like Tree1Likes

Thread: If Statement Modification (Using String Methods)

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

    Default If Statement Modification (Using String Methods)

    As you know, pig latin is a language all about vowels in the English language. hasAVowel() is a function that needs to be created to locate and return the index number of where a vowel is inside of a word.

    I have been emailing my teacher here and there about this pig latin program and he strictly writes in his directions:

    "Hints: You should not use a gigantic if/else compound statement or ladder in the function 'hasAVowel()' "


    This is my current code for the function:
    Java Code:
    public static int hasAVowel(String word){//detects if the word has a vowel and returns location
    
            char letter=0;
            int index=0, num=0;
            for(int i=0; i<word.length(); i++){
                letter=word.charAt(i);
                if(letter=='a' || letter=='i' || letter=='o' || letter=='e' || letter=='u' || letter=='A' || letter=='I' || letter=='O' || letter=='E' || letter=='U'){
                    index=i;
                    i=word.length();
                    num=1;
                }
            }
            if(num==0){
                index=-1;
            }
    
    
            return index;
    
    }
    (Just to help you understand, -1 means no vowel, 0 means vowel is first character, >0 means vowel is anywhere else in the string)

    I emailed him about it and it is obviously not what he is looking for. He replies saying his suggestion is to use some string methods like length,substring,indexOf but obviously "I'm not going to tell you. That's part of the learning process for this assignment" (his exact words).

    The notes I took in his class have only shown us the following methods:

    .length()
    .substring()
    .indexOf()
    .equals()
    .charAt()
    .compareTo()
    .split()

    How would it be possible to mimic the above if statement more concisely using one of these methods?

    Cheers.
    Last edited by danthegreat; 09-11-2011 at 12:20 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    That's part of the learning process for this assignment" (his exact words).
    Hint: Put all the vowels in a String

  3. #3
    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 broke my femur so I haven't been to class yet and just copied notes down from a friend but I don't really know what .IndexOf(string) does? Can anyone explain with an example what that does to strings in the first place?

    Thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statement Modification (Using String Methods)

    Quote Originally Posted by danthegreat View Post
    I broke my femur so I haven't been to class yet and just copied notes down from a friend but I don't really know what .IndexOf(string) does? Can anyone explain with an example what that does to strings in the first place?
    Better still, let me link you to a great resource that will allow you to read about any and all classes that are part of core Java, the Java API. Click on the link and then click on the entry for String, then search for the indexOf(...) method. It is a wonderful resource, and the sooner you start using it, the quicker you'll become a better programmer.

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

    Default Re: If Statement Modification (Using String Methods)

    Do you have a copy of the API doc or a link to it? If not here is a link:
    Java Platform SE 6

    You need to learn how to find the doc for any java class.
    The classes are listed in the lower left. Scroll down to the one you want and click on it and its doc will come into the right hand frame.

    A way to see what a method like this does is to write a small test program, use the method and printout the results. Change the program and do it again. and so on.

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

    Default Re: If Statement Modification (Using String Methods)

    Ok so I read about it and tested it out in Ecllipse to see if I understand it:

    This is my sample code:
    Java Code:
            String phrase="Test";
            String str="e";
                
    System.out.print(phrase.indexOf(str));
    The output is 1 which is correct. however would this mean I need to repeat this line over and over again to mimic my code above, for example:
    Java Code:
    String phrase="aeiou";        
        
                
            System.out.print(phrase.indexOf("a"));
            System.out.print(phrase.indexOf("e"));
            System.out.print(phrase.indexOf("i"));
            System.out.print(phrase.indexOf("o"));
            System.out.print(phrase.indexOf("u"));
            System.out.print(phrase.indexOf("A"));
            System.out.print(phrase.indexOf("E"));
            System.out.print(phrase.indexOf("I"));
            System.out.print(phrase.indexOf("O"));
    System.out.print(phrase.indexOf("U"));
    is there a way for IndexOf to scan multiple letters like:
    system.out.print(phrase.indexof("aeiouAEIOU");

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    is there a way for IndexOf to
    These types of questions are often answered by going to the API doc and reading.
    Sometimes a method will be overloaded and be able to do lots of different things. Go see what this one has.

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

    Default Re: If Statement Modification (Using String Methods)

    The only thing I see when reading is:

    IndexOf(string)<same as above
    IndexOf(string, int fromIndex)<allows you to start from a different index

    Doesn't really say anything about searching more than one character..Any help?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    Doesn't really say anything about searching more than one character
    If it doesn't say, then it doesn't do it. Sometimes the doc can take some interpretation.
    Be sure to copy the doc here and to ask if you don't understand what it is saying.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statement Modification (Using String Methods)

    Myself, I'd loop through my String using charAt(int index), and then see if the char is contained in a constant String VARIABLES = "AEIOUaeiou". To see if VARIABLES contains the char of interest, I'd use the API to select an appropriate String method.

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

    Default Re: If Statement Modification (Using String Methods)

    Ok so I tried to think about what is possible and then .compareTo came to mind...


    According to the API, this is what the return value is:
    the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

    I don't really understand the bolded parts. Am I at least on the right track??

    This is also what I have in code:
    Java Code:
    String vowels = "aeiouAEIOU";		
    		for(int i=0; i<word.length(); i++){
    letter=word.charAt(i);

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    What Strings are you comparing when you use compareTo?

    What do you want the code you posted to do? It seems to be missing the end of the loop.

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

    Default Re: If Statement Modification (Using String Methods)

    Maybe comparing the variable 'vowels' and 'letter' to see if the letter is the same as the vowel?

    I want the code to detect the index of the vowel in the word.

    I tried doing sample code:
    Java Code:
    String word = "Hello";		String vowels = "aeiouAEIOU";
    		String letter="";
    		
    		for(int i=0; i<word.length(); i++){//loop through all the letters
    			letter=letter+word.charAt(i);
    			System.out.println(vowels.compareTo(letter));
    		    System.out.println(letter);
    			letter="";
    		
    }
    output results:
    25
    H
    -4
    e
    -11
    l
    -11
    l
    -14
    o

    Don't suppose that's right...lol can I have any other hints?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    see if the letter is the same as the vowel?
    The equals method would be more direct.

    can I have any other hints?
    What do you want to do?
    You know how to get a single letter from a String.
    Now how would you determine if that letter is a member of a set of letters?
    See the hint in post#2
    Last edited by Norm; 09-11-2011 at 03:06 AM.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statement Modification (Using String Methods)

    Here's some pseudo-code that explains the logic that I've used in this situation

    Java Code:
    Loop from i = 0 to < the length of the String
       get the ith char, c, from the String
       create a String variable, cAsString, and convert c to this String variable. (String has a valueOf method for this).
       check to see if the vowels String contains cAsString. String has a contains method (note that I highlighted this in red as a hint).
          If true, then you've found your index
       end of if block
    end of for block
    sunde887 likes this.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: If Statement Modification (Using String Methods)

    @Fubarable: in this case cAsString is unnecessary as indexOf() works as a "is character in string?" check.

    Post 3 reinstated (for what its worth):

    You are using charAt() to "pick out" one letter at a time. And that looks OK to me.

    Frankly what you are being told to find is a "trick": Can you think of some other string (not word) that you can use with indexOf() to tell if letter is a vowel?

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: If Statement Modification (Using String Methods)

    Quote Originally Posted by pbrockway2 View Post
    @Fubarable: in this case cAsString is unnecessary as indexOf() works as a "is character in string?" check.
    But contains doesn't, and I thought I did check this before posting.

    edit: oh, I see. You're suggesting use of indexOf instead of contains and just checking that the result is > 0.

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

    Default Re: If Statement Modification (Using String Methods)

    Ok so after trying to understand what I'm trying to do this is what I've come up with:

    Java Code:
    String word = "Hello";		String vowels = "aeiouAEIOU";
    
    
    		
    		for(int i=0; i<word.length(); i++){//loop through all the letters
    			if(vowels.indexOf(word.charAt(i))!=-1){
    
    
    				System.out.println(vowels.indexOf(word.charAt(i)));
    				i=word.length();
    			}
    
    
    
    
    
    
    		
    		}
    
    
    
    
    
    
    		
    }
    This is my output:
    1


    if I enter "Daniel" instead of "Hello" for phrase, the output becomes a 0 which is wrong..Am I on the right track?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default Re: If Statement Modification (Using String Methods)

    This is my output:
    1
    What is meaning of your output? What is the value that is printed?

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

    Default Re: If Statement Modification (Using String Methods)

    that means 1 is the index of the first vowel seen which is exactly what i want it to do. however when i put daniel it outputs 0 which is the letter d

Page 1 of 2 12 LastLast

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
  •