Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pig latin question

 
Ed Buckman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm pretty new to java and I am working on a set of problems. The first one is about Pig Latin and I have a little bit of a problem. An IO file is given Here is the question:

------------------------------------------------------------------
Write a program which will ask the user for a single English word and "translate" it into Pig Latin. The rules for this translation are:

* If the English word begins with a consonant, move the consonant to the end of the word and add "ay". E.g., the English word "nix" becomes "ixnay" in Pig Latin.

* If the English word begins with a vowel, simply add "way" to the end of the word. E.g., the English word "apple" becomes "appleway" in Pig Latin.

And the sample runs are:

java PigLatin
clam
The result is lamcay

java PigLatin
oyster
The result is oysterway

java PigLatin
soft-shelled crab
User entered bad input.

java PigLatin
[user types nothing and presses Enter]

User entered bad input.
----------------------------------------------------------

There is an IO.class file that is given with the following methods:

# readString : lets the user enter some text, and returns it as a string
# printString : displays a string result on the screen
# reportBadInput : prints an error message indicating that the user entered a value that makes the program's job impossible.


And my code is:

String str = IO.readString();

if (str.startsWith("a") | str.startsWith("e") | str.startsWith("i") | str.startsWith("o") | str.startsWith("u")){
IO.printString(str.concat("way"));
}
else{
//

}

Now I could do part 1 of the problem but the part 2 is a bit tricky and also I am not sure how to handle bad input. Any help would be greatly appreciated.


Thanks.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like for bad input you would check for a non-alpha character or and empty String.
 
Ed Buckman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok and how would I do that. Keeping in mind I am a complete newbie to java. Also in the original post I said I was having problems with part 2 but its actually part 1 that is tricky:

"If the English word begins with a consonant, move the consonant to the end of the word and add "ay". E.g., the English word "nix" becomes "ixnay" in Pig Latin."
 
Henry Wong
author
Marshal
Pie
Posts: 21117
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You may want to use "||" instead of "|". There is no reason to go through all the conditionals if the first one suceeds.

BTW, this project is like a "blast from the past" for me. One of my college projects was to do a piglatin converter. Of course, back then, I had to do it in Fortran.

Henry
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your friend. Look up String and that should get you there.
 
Ed Buckman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. I used "||" instead of "|" and Here is the link for the documentation.

For the first part I know if a word starts with a consonant then I cut the first character of the word and put it at the end of the word and to that concatenate "ay" to it. Just cant figure out the code for it.


Oh btw... thanks for all your help guys.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take it one step at a time.

First lets check for bad input. I'll point you to two references and see if you can figure it out.

First look at the matches method in String and then look here

Lets see where you get with that.

[Edit:] Just a hint. In this case it's easier to check for good input.
[ May 19, 2005: Message edited by: Steven Bell ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic