This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Pig latin question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
author
Posts: 23924
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic