• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pig Latin Program  RSS feed

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I must write a program that converts a sentence into pig latin. I have to have the main method, then 4 other methods. One must be called translatesentence that calls 2 other methods called removeContractions and translateWord. translateWord will convert the word into pig latin but must call findFirstVowel that must return the position of the first vowel. My code so far does not work, is incomplete, and probably doesn't make sense. I'm not sure what to do or how to improve. Any help will be much appreciated.
 
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some pointers:

use the String's equals() method to compare strings, don't use "==" (eg in the method removeContractions())

For findFirstVowel() method, have a look at at String's firstIndexOf() method.



you should need to have an idea what the output is given the input in order to continue or you will just go nowhere.

For example: If the user enter the line:
I can't program Java because it's too difficult to learn.

What will the translateSentence() method output?
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like this?

For the FirstVowel, would this work?
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:My code so far does not work, is incomplete, and probably doesn't make sense. I'm not sure what to do or how to improve. Any help will be much appreciated.]

You've already got some good tips, but mine is much more generic: StopCoding (←click).

My advice: write out a sentence: eg: "the quick brown fox jumps over the lazy dog". Now, forget all about Java and translate that into Pig-Latin yourself.
NOW work out what you did, and translate THAT into Java.

For example: is there a Pig-Latin translation of "the"? If not, why not? What about "fox" ... or "over" ... or "lazy"...?

BEFORE you can write a program, you need to understand the problem, so deal with that, and describe it in English (or your native tongue) before you write your first line of Java code.

HIH

Winston
 
marco cucinotta
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to restart the program step by step. This, so far, should just return the position of the vowel and if the first letter is a vowel, will print the word + way. But the output is [Ljava.lang.String;@29214726-way
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
marco cucinotta wrote:But the output is [Ljava.lang.String;@29214726-way

That's because you are trying to print a String array, not an individual String.
What are you actually trying to do ?
  • Print all the contents of the array with -way appended at the end ?
  • or
  • Print an individual word with -way appended to it ?
  •  
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I am trying to print an individual word with -way at the end. So if the sentence "I eat cake" is entered, the output should be "I-way eat-way ake-cay". For right now I am trying to get the first vowel words to print.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:For right now I am trying to get the first vowel words to print.

    If you are only checking the first letter of a word, why are you using i as a parameter to the charAt method ?
    At the moment, the first time round the loop of the firstVowel method, you check the first letter of the first word, the second time you check the second letter of the second word, the third time you check the third letter of the third word, etc.

    Your firstVowel method should really be returning a boolean and should only check one word.
    Something like (without the method - I'll let you do that bit)
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I did that because the method findFirstVowel has to return the position of the first vowel, not add the "-way". That will be done in the translateWord method.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:I did that because the method findFirstVowel has to return the position of the first vowel, not add the "-way".

    But the position of the first vowel in what ? At the moment you are just checking the first position of the first word, the second position of the second word, the third position of the third word, etc.
    Think about what that method is actually doing because I'm pretty sure it's not doing what you want it to do.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:So if the sentence "I eat cake" is entered, the output should be "I-way eat-way ake-cay"

    If this is what you want then the structure of your code should be something like

    So you should only be passing a single word to your firstVowel method, not the entire array of words.
    The for loop needs to be in your translateword method.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are right and your mock setup is what I am looking for but I don't know how to examine each word individually.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:I don't know how to examine each word individually.

    Yes you do. You're doing it currently in your firstVowel method. All I'm saying is that the loop in the firstVowel method should actually be in your translateword method and instead of passing the whole array to firstVowel, you just pass individual words.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see your point but if I remove the foor loop in the findFirstVowel, what do I do about the i's inside the array? They are not initialized
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Joanne Neal wrote:instead of passing the whole array to firstVowel, you just pass individual words.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This fixed the error, could this work? A bracket is off so it's not running but wondering if this is acceptable?
     
    Sheriff
    Posts: 22845
    43
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No...

    Joanne Neal wrote:...instead of passing the whole array to firstVowel, you just pass individual words...


    That's good advice, and you're still not doing it. Notice that the parameter to firstVowel is an array of Strings? Don't do that. Just pass one String to the firstVowel method. That would require you to write a loop which goes through the array of Strings and passes each of them to firstVowel, one at a time.

    Edit: Actually I see you already have that loop. That simplifies your task considerably. Just change firstVowel to take a String as its parameter, and pass the current String that the loop is looking at into that method.

    Another edit: Think about your program in English words (or words in whatever language you prefer to think in). You need to find the first vowel in each word, right? So the firstVowel method should be given a word.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Met with my professor today and he said the code is good but can't use split method, have to use loop. Here's revised code without the loop.

     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Okay, you seem to have made some progress.
    What I would suggest now is that you forget about reading in some text and splitting it up so that you can loop through it.
    For the moment, just add the following method to your code

    and change lines 2 - 6 of your main method to

    That will give you a hardcoded list of words you can use to develop the rest of your program and we can sort out how to actually split the text later once everything else is working.
    So your main method should now look like this

    See if you can write the translatesentence method. Notice that I am passing a String array to it, so this is the method where you will need to have your loop. In this loop you should call translateword with each word and append the result of each call to a String. You should then return this String at the end.

    Have a go at that and see what results you get.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'd like to follow your suggestions but I don't want to confuse myself. I can translate a single word into the appropriately changed word, but it doesn't analyze anything after the first word. I tried different loops, but they are not changing anything. I also added in the removeContractions method, but need to send the result to the firstVowel, etc.
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:I can translate a single word into the appropriately changed word, but it doesn't analyze anything after the first word.

    That's because you are returning from the translateSentence method after processing one word (line 16). You should be appending the value returned from translateword to your piglatin string and then returning that after the for loop.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So how would I do that?
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:So how would I do that?

    Which ? Append one String to another String or return a String from a method ?
    As examples of both exist in the various bits of code in this thread, I'm afraid you're going to have to show some effort this time.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Would I make a while loop like (while(words < line.length()) in the translatewords or translateSentence method? This would keep reading the words until the original length of the string is up
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:Would I make a while loop like (while(words < line.length()) in the translatewords or translateSentence method? This would keep reading the words until the original length of the string is up

    What ?? I've already told you how to fix the problem of only the first word being processed. It will show up another problem with your code but at least it will be a step forward.
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:Would I make a while loop like (while(words < line.length()) in the translatewords or translateSentence method? This would keep reading the words until the original length of the string is up...

    I'll say it again, Marco: StopCoding (←click).

    Joanne's giving you some great advice, but I suspect that you're tying yourself in knots because you're trying to apply it to the code you've already written - ie, you're trying to force it to do what you want; and that generally means that you're too close to it all.

    Sometimes, when you're really stuck on a problem (and this doesn't just apply to programming), the best thing to do is to just leave it alone for a while. Chill out; have a beer (if you're old enough ) or go outside and play some footie. Or just get a good night's sleep. I can't tell you how many "Eureka" moments I've had at four in the morning...

    Either way, I'd suggest you turn your computer OFF, step back, and write out in English (or your native language) and in detail, exactly what you need to do to translate ONE WORD into Pig Latin. Correctly. Every time. Imagine you were trying to explain it to a 10-year old kid.

    Then describe exactly how you break up a sentence into words - and if you can't use split(), you'll have to come up with another way.

    Put those two together, and you'll have a solution for translating a sentence into Pig Latin.

    And once you've done that, turn your computer back on and see how closely your current code matches your description.

    Programming isn't about coding, it's about thinking, and understanding.

    HIH

    Winston
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Worked with my professor and came up with this. But at time of translation, currentword is empty.
     
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry to pile on but you really have to listen to what Winston is telling you: StopCoding for a while and think about what you're trying to tell the computer to do.
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I know what I want the computer to do, it's me telling the computer what to do is the problem. This is my first course in programming and I have this project and another lab due on Friday so that's why I'm rushing on this. I understand people don't want to just give me the answer but help, but just telling me to stop coding isn't very helpful to me. I'm not trying to be harsh I'm just stressing over this seemingly easy program that is difficult for me.
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, then let me translate for you what you're telling the computer with this code and you can adjust your instructions to match what you actually have in mind.

    I'm pretty sure those are not the instructions you want to give to the computer. You have to think of the following:
    1. When do I add a character to the currentword?
    2. When do I clear out currentword and start building up a new one?
    3. When do I know that the currentword is complete and I can translated it now?
     
    Junilu Lacar
    Sheriff
    Posts: 11494
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Another tip: It will help if you use a boolean that will tell you whether you're currently in a word or not.

    Your logic will involve asking "I am in a word?" and taking appropriate action based on the answer. Combine that with the other questions I mentioned in my last post and you should be able to come up with a set of instructions that will get the computer to do what you want it to do. Again, you have to StopCoding first and lay out these questions logically and know exactly what you should do and when to do it.

    Another tip on how to code with a boolean:

     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    marco cucinotta wrote:I know what I want the computer to do, it's me telling the computer what to do is the problem.

    Then I'm sorry, but you DON'T know what you want the computer to do - or at least you haven't written it out in enough detail.

    This is my first course in programming and I have this project and another lab due on Friday so that's why I'm rushing on this. I understand people don't want to just give me the answer but help, but just telling me to stop coding isn't very helpful to me.

    Actually, I've also given you a link. Did you read it?

    I'm not trying to be harsh I'm just stressing over this seemingly easy program that is difficult for me.

    And the problem is that "seemingly" bit. Not everything that seems easy actually is.

    For example: Sorting anywhere up to to about 7 or 8 objects (eg, letter blocks) is incredibly simple for us humans, because we can process an awful lot of information visually without even thinking about it. Writing a program to sort those same 7 letters is much more difficult, because you have to tell the computer every single step involved - including ones that you probably take for granted when you're just rearranging blocks yourself.

    However, once you've written it (and assuming you got it right ), that same program can now sort a million blocks - way beyond anything we can do.

    And that's why I say that you need to understand the process. Completely. And I promise I'm not trying to make life difficult for you.

    When you're writing something like "Hello world", you can simply sit down and start coding; but as soon as problems start getting more complex you need to step back and write things down. Now it's possible you might be able to get this program to work as you're going, but if you do you'll simply be reinforcing a habit that you really need to break as soon as possible.

    Winston
     
    marco cucinotta
    Ranch Hand
    Posts: 44
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Forgot to say I already figured it out. It works perfectly. Thanks everyone
     
    Master Rancher
    Posts: 2045
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    arcomay ucinottacay,

    despite all our urgent advices to StopCoding, you nevertheless went ahead.
    Well, you are now faced with the consequences: you solved it.
    Let this be a lesson for you!

     
    Marshal
    Posts: 4051
    239
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Marco, It's great that you got your problem solved.

    What would be even better is for you to share your solution here so everybody can see how you solved it. It would close off this thread just nicely.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!