1 2 Previous Next 16 Replies Latest reply: Jan 28, 2009 4:40 PM by 843785 RSS

    Experimenting with looping through array

    843785
      I've got this piece of code to check if some input of monosyllabic or not. Hope I can explain my problem with it. What it now does, is keep track of every first and last position of a vowel in the input. For a word like "bed", it works fine. The first position of an 'e' is the same as the last one, so it says "input is monosyllabic. (I'm not writing the program for English, so for those of you wondering how to recognize the monosyllabicity of "race" for example, that's irrelevant). And of course I still need to account for words like "breed", with two consecutive vowels.

      The thing is however, that the for loop keeps track of the first and last position of every vowel in the array. If a vowel from the array is not in the input, it prints "-1". Now what I want it to do, is keep track of the first and last position for 'a' vowel, and not for every vowel. So now for an input like "universe", the println(first/lastVowel) prints several lines. Being the first and last position of 'u', the first and last of 'i' and of 'e'. Because the first and last position of 'u' is equal, it also prints "input is monosyllabic".

      How do I let the for loop only keep track of the first vowel and the last vowel it encounters, and not creating a specific integer value for each vowel defined in the array?

      {code}char [] vowels = {'a', 'e', 'i', 'o', 'u'};
           
           for (char item : vowels){
                int firstVowel = input.indexOf(item);
                int lastVowel = input.lastIndexOf(item);
                
                System.out.println(firstVowel);
                System.out.println(lastVowel);
           
                if (!(firstVowel==-1) && !(lastVowel==-1) && firstVowel==lastVowel){
                     System.out.println("input is monosyllabic");
                }
           }{code}
        • 1. Re: Experimenting with looping through array
          800282
          Igor_Pavlove wrote:
          ...
          How do I let the for loop only keep track of the first vowel and the last vowel it encounters, and not creating a specific integer value for each vowel defined in the array?
          ...
          Write your own method that does this:
          public static Character firstChar(char[] lookingFor, String text) {
            // loop from 'i'=0 -> text.length()
            //   for each character from 'text' at index 'i' check if it's in 'lookingFor', if so, return that character
            // end loop
            // return null
          }
          • 2. Re: Experimenting with looping through array
            843785
            Igor_Pavlove wrote:
            I've got this piece of code to check if some input of monosyllabic or not. Hope I can explain my problem with it. What it now does, is keep track of every first and last position of a vowel in the input. For a word like "bed", it works fine. The first position of an 'e' is the same as the last one, so it says "input is monosyllabic. (I'm not writing the program for English, so for those of you wondering how to recognize the monosyllabicity of "race" for example, that's irrelevant). And of course I still need to account for words like "breed", with two consecutive vowels.

            The thing is however, that the for loop keeps track of the first and last position of every vowel in the array. If a vowel from the array is not in the input, it prints "-1". Now what I want it to do, is keep track of the first and last position for 'a' vowel, and not for every vowel. So now for an input like "universe", the println(first/lastVowel) prints several lines. Being the first and last position of 'u', the first and last of 'i' and of 'e'. Because the first and last position of 'u' is equal, it also prints "input is monosyllabic".

            How do I let the for loop only keep track of the first vowel and the last vowel it encounters, and not creating a specific integer value for each vowel defined in the array?

            {code}char [] vowels = {'a', 'e', 'i', 'o', 'u'};
                 
                 for (char item : vowels){
                      int firstVowel = input.indexOf(item);
                      int lastVowel = input.lastIndexOf(item);
                      
                      System.out.println(firstVowel);
                      System.out.println(lastVowel);
                 
                      if (!(firstVowel==-1) && !(lastVowel==-1) && firstVowel==lastVowel){
                           System.out.println("input is monosyllabic");
                      }
                 }{code}
            You have a couple of problems with this:

            1 - notice that your firstVowel and lastVowel variables are defined within the scope of your for loop so they fresh and new for each iteration of the loop. So if you never get any result other than that of 'u'. (except for the print, but you don't have any results you can actually use)

            2 - for firstVowel, you just need to check if your return from your (indexOf is less than firstVowel) and (firstVowel not equal to -1): for your lastVowel you do the same but check for greater than.

            To accomplish this you have to take the definition of your firstVowel and lastVowel outside of your for loop, you'll also need to use a variable to catch your indexOf so you can do the comparison for firstVowel and lastVowel.
            • 3. Re: Experimenting with looping through array
              843785
              Ok, thanks a lot.

              Think I do get your answer if you made a minor mistake in your point 2: "for firstVowel, you just need to check if your return from your (indexOf is less than firstVowel)". Shouldn't that last bit be "indexOf is less than lastVowel"? If so, I think I get it. If not, need to think about it some more....
              • 4. Re: Experimenting with looping through array
                843785
                I'm obviously confused and don't understand the requirements. I also do things backwards sometimes. But if I had to write the code that I think you're writing, then I would:
                create a method called "isVowel"
                loop through the string starting at the first char calling the isVowel method until the first vowel is encountered
                store the value of the index
                exit the loop
                loop throught the string starting at the last char calling the isVowel method until a vowel is encountered
                store the value of the index
                exit the loop
                examine the two indeces

                kr
                walken

                Edited by: walken16 on Jan 28, 2009 3:35 PM
                to correct grammatical error(omission) shown in bold font
                • 5. Re: Experimenting with looping through array
                  843785
                  and it would probably be more efficient on words like
                  dichlorodifluoromethane
                  • 6. Re: Experimenting with looping through array
                    843785
                    Now that I read again that is I think indeed what Prometheuz was also referring to. Shouldn't this then be done something like this?

                    String vowels = "aeiou";
                          int firstVowel = 0;
                          
                          for(int i=0; i<input.length(); i++){
                               if (!(vowels.indexOf(i)==-1)){
                                    firstVowel = i;
                               }
                          }
                    Or am I completely missing something?
                    • 7. Re: Experimenting with looping through array
                      843785
                      for(int i=0; i<input.length(); i++){
                      if (!(vowels.indexOf(i)==-1)){//don't think this will work, you need the char at i
                      firstVowel = i;
                      }
                      use input.charAt(i); if i recall correctly...

                      Edited by: walken16 on Jan 28, 2009 3:56 PM

                      Edited by: walken16 on Jan 28, 2009 3:57 PM

                      Edited by: walken16 on Jan 28, 2009 3:59 PM
                      • 8. Re: Experimenting with looping through array
                        843785
                        possible isVowel method
                            private boolean isVowel(char c) {
                                boolean ret = false;
                                if("aeiouAEIOU".indexOf(c, 0)>=0)
                                    ret = true;
                                return ret;
                            }
                        • 9. Re: Experimenting with looping through array
                          843785
                          Igor_Pavlove wrote:
                          Or am I completely missing something?
                          that's close depending on your requirements. you need a second loop going backward for the last index and you need to fix the "char" thing
                          • 10. Re: Experimenting with looping through array
                            843785
                            walken16 wrote:
                            possible isVowel method
                            private boolean isVowel(char c) {
                            boolean ret = false;
                            if("aeiouAEIOU".indexOf(c, 0)>=0)
                            ret = true;
                            return ret;
                            }
                            private boolean isVowel(char c) {
                                return "aeiouAEIOU".indexOf(c) != -1;
                            }
                            • 11. Re: Experimenting with looping through array
                              843785
                              Thanks. With the charAt added it works.
                              • 12. Re: Experimenting with looping through array
                                843785
                                BigDaddyLoveHandles wrote:
                                private boolean isVowel(char c) {
                                return "aeiouAEIOU".indexOf(c) != -1;
                                }
                                Thanks for the one liner. Considering present company, i thought an expanded version might be a bit clearer and also leave something to the imagination.... we need to sit down and have a chat about Generics sometime...

                                kr
                                Walken

                                Edited by: walken16 on Jan 28, 2009 4:12 PM
                                • 13. Re: Experimenting with looping through array
                                  843785
                                  Igor_Pavlove wrote:
                                  Now that I read again that is I think indeed what Prometheuz was also referring to.
                                  can't say for sure about that but i know that Prometheuz is famous... I read a rhyme on this forum one time about Prometheuz.... i'm sure it's archived by now though .....
                                  • 14. Re: Experimenting with looping through array
                                    843785
                                    Shit man, sorry to spam the forum, but I get an error I don't understand. Yes, I'm a real noob:).

                                    So I flipped it around a little, didn't get it to work the way it should, so I know the folling code is in that sense inefficient in dat the first for loop loops all the way till the last vowel and reminds that one. So I want to loop the other way round, starting at the end, then going all till the first vowel and remind that one. Speed and efficiency is not so much an issue in my case....

                                    String vowels = "aeiou";
                                          int lastVowel = 0;
                                          int firstVowel = 0;
                                         
                                          for(int i=0; i<input.length(); i++){
                                               if (!(vowels.indexOf(input.charAt(i))==-1)){
                                                    lastVowel = i;
                                               }
                                          }
                                          for (int k=input.length(); k>0; k--){
                                               if (!(vowels.indexOf(input.charAt(k))==-1)){
                                                    firstVowel = k;
                                               }
                                          }
                                    If I run that, I get:
                                    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
                                         at java.lang.String.charAt(Unknown Source)

                                    What's the matter?
                                    1 2 Previous Next