How do I make a pig latin program that DOESNT glitch out on a single charecter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • berky
    New Member
    • Jan 2011
    • 16

    How do I make a pig latin program that DOESNT glitch out on a single charecter?

    I am so very close to making this program happen, however I am running into an error with single character words.


    When My program runs, if I put a single letter word in like

    A or I

    then it glitches out on me! something about it going out of bounds...what should I do? I'm trying to declare an if statement that will fix the problem "if length >2, then print as something"

    but I CANT get it to work...please help

    import java.util.Scann er;
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Where's the if length part of the code you mentioned?

    Comment

    • berky
      New Member
      • Jan 2011
      • 16

      #3
      this is the error message

      Comment

      • berky
        New Member
        • Jan 2011
        • 16

        #4
        the if statement below the Q

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          Well, what you really want is .length()>1, not 2. And the length check needs to go before everything else and if the length is 1, then you need to skip the rest of the pig latin transformation.

          Comment

          • berky
            New Member
            • Jan 2011
            • 16

            #6
            I put it to the front of the for loop and set it to 1. I still get the error, and now my vowel statement doubles up :/

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              You put the if outside the for loop? It needs to be inside the for loop but before everything else.

              Comment

              • berky
                New Member
                • Jan 2011
                • 16

                #8
                oh no, I put it in the for loop before everything. Set it to 1 and I continue to get this strange error :/. Whats worse it I dont comment it out then it ends up screwing up my other if statements and making them print additional things.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  I think you misunderstood my meaning. What you want to do is something akin to the following. Note that what follows is only pseudocode.
                  Code:
                  For Each word In wordList
                    If word.length == 1 Then
                      pigLatin = pigLatin + word
                    Else
                      // Rest of code
                    End If
                  Next word

                  Comment

                  • berky
                    New Member
                    • Jan 2011
                    • 16

                    #10
                    I...THINK I did what you said. Which did fix the reapeat code error, however I am still getting the "I A, single character" error.It says Im going out of bound, which is strange considering I dont think I gave it a bound.

                    Comment

                    • Rabbit
                      Recognized Expert MVP
                      • Jan 2007
                      • 12517

                      #11
                      Your x, y, z, q, m, and qu variables are still in the wrong place. They need to be inside the else part of the if.

                      You don't give a string a bound, a string is automatically bound by it's length.

                      Comment

                      • berky
                        New Member
                        • Jan 2011
                        • 16

                        #12
                        oh my goodness...IT WORKS! THANK YOU SO MUCH FOR YOUR HELP!!!

                        Comment

                        • berky
                          New Member
                          • Jan 2011
                          • 16

                          #13
                          I wonder though, the variables where still in the for loop, why did that mess things up?

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            The for loop has nothing to do with the error. It's because it was outside the if section that was testing for string length.

                            If your string is one character long, and you're trying to get the second character of a one character string, you're going to go out of bounds.

                            Code:
                            Index [0] [1] [2]
                                   A
                                   B   A   R
                            For the first string, you're trying to get the character at index 1. There is no character at index 1. You are out of bounds.

                            Comment

                            • berky
                              New Member
                              • Jan 2011
                              • 16

                              #15
                              interesting, I though they where global and worked throughout the function. Would I get the same effect if I put them out of the for loop and in the beginning of the program?

                              Comment

                              Working...