Results 1 to 20 of 30
- 09-11-2011, 12:18 AM #1
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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:
(Just to help you understand, -1 means no vowel, 0 means vowel is first character, >0 means vowel is anywhere else in the string)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; }
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.
- 09-11-2011, 12:29 AM #2
Re: If Statement Modification (Using String Methods)
Hint: Put all the vowels in a StringThat's part of the learning process for this assignment" (his exact words).
- 09-11-2011, 12:46 AM #3
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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.
-
Re: If Statement Modification (Using String Methods)
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.
- 09-11-2011, 12:50 AM #5
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.
- 09-11-2011, 01:06 AM #6
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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:
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="Test"; String str="e"; System.out.print(phrase.indexOf(str));
is there a way for IndexOf to scan multiple letters like: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"));
system.out.print(phrase.indexof("aeiouAEIOU");
- 09-11-2011, 01:09 AM #7
Re: If Statement Modification (Using String Methods)
These types of questions are often answered by going to the API doc and reading.is there a way for IndexOf to
Sometimes a method will be overloaded and be able to do lots of different things. Go see what this one has.
- 09-11-2011, 01:20 AM #8
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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?
- 09-11-2011, 01:21 AM #9
Re: If Statement Modification (Using String Methods)
If it doesn't say, then it doesn't do it. Sometimes the doc can take some interpretation.Doesn't really say anything about searching more than one character
Be sure to copy the doc here and to ask if you don't understand what it is saying.
-
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.
- 09-11-2011, 02:36 AM #11
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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);
- 09-11-2011, 02:45 AM #12
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.
- 09-11-2011, 02:59 AM #13
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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:
output results: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=""; }
25
H
-4
e
-11
l
-11
l
-14
o
Don't suppose that's right...lol can I have any other hints?
- 09-11-2011, 03:04 AM #14
Re: If Statement Modification (Using String Methods)
The equals method would be more direct.see if the letter is the same as the vowel?
What do you want to do?can I have any other hints?
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#2Last edited by Norm; 09-11-2011 at 03:06 AM.
-
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
- 09-11-2011, 03:47 AM #16
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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?
-
- 09-11-2011, 02:15 PM #18
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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:
This is my output: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(); } } }
1
if I enter "Daniel" instead of "Hello" for phrase, the output becomes a 0 which is wrong..Am I on the right track?
- 09-11-2011, 02:22 PM #19
Re: If Statement Modification (Using String Methods)
What is meaning of your output? What is the value that is printed?This is my output:
1
- 09-11-2011, 02:24 PM #20
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Similar Threads
-
Calling Methods in switch Statement
By borth92 in forum New To JavaReplies: 7Last Post: 12-09-2010, 09:14 AM -
Using the return statement to display a string
By Hoverboy in forum New To JavaReplies: 6Last Post: 11-17-2010, 09:22 AM -
If Else Statement using String
By j@v@ in forum New To JavaReplies: 2Last Post: 10-08-2010, 04:29 PM -
Let eclipse warn about a semicolon after an if statement and string == string?
By foobar.fighter in forum EclipseReplies: 5Last Post: 01-11-2009, 10:12 AM -
Sql string with callable statement..
By nathan in forum JDBCReplies: 1Last Post: 09-24-2008, 01:41 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks