This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Pig latin question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pig latin question" Watch "Pig latin question" New topic
Author

Pig latin question

Ed Buckman
Greenhorn

Joined: Mar 21, 2004
Posts: 5
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

Joined: Dec 29, 2004
Posts: 1071
Seems like for bad input you would check for a non-alpha character or and empty String.
Ed Buckman
Greenhorn

Joined: Mar 21, 2004
Posts: 5
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
Sheriff

Joined: Sep 28, 2004
Posts: 18499
    
  40



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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
This is your friend. Look up String and that should get you there.
Ed Buckman
Greenhorn

Joined: Mar 21, 2004
Posts: 5
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

Joined: Dec 29, 2004
Posts: 1071
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pig latin question
 
Similar Threads
Can someone test my game again?
translate to pig latin!? completely lost....
StringTokenizer question
why can't some people play my game
Problem with looping... Arg