Results 21 to 30 of 30
- 09-11-2011, 02:27 PM #21
- 09-11-2011, 02:51 PM #22
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Re: If Statement Modification (Using String Methods)
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?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(); } }Last edited by danthegreat; 09-11-2011 at 02:53 PM.
- 09-11-2011, 02:53 PM #23
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.
- 09-11-2011, 02:56 PM #24
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
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'?
- 09-11-2011, 02:59 PM #25
Re: If Statement Modification (Using String Methods)
That is not clear since 'a' is in both the word String and the vowels String.2 represents the 'a' found.
A clearer way to say this is that 2 is the index into one of these String: word or vowels
- 09-11-2011, 02:59 PM #26
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?
- 09-11-2011, 03:01 PM #27
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Re: If Statement Modification (Using String Methods)
sorry that was a typo its vowels.length()
- 09-11-2011, 03:05 PM #28
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.
- 09-11-2011, 03:10 PM #29
Member
- Join Date
- Sep 2011
- Location
- Washington DC
- Posts
- 51
- Rep Power
- 0
Re: If Statement Modification (Using String Methods)
I figured it out: (This is my final hasAVowel() Function) -
pos is a new variable that makes sure the earliest vowel is saved. For example: "Beams"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; }
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.
- 09-11-2011, 03:51 PM #30
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