Write a program in JAVA to accept a sentence
convert it into PIGLATIN form and display
the new
sentence.
Answers
Stack Overflow
sign up log in
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Questions Jobs Tags Users Badges Ask
up vote
-1
down vote
how to change all words in a sentence on piglatin.java
java
I'm writing program called piglatin. The program will keep running until user type quit. Any words start with a vowel, the end of the word must add "way". If the word start with a consonant, then I have to move the consonant to the end of the word and add word "ay". The only problem i have is how to make my program to be able to change all the words individually instead of based on the first word of a sentence. For example "he is nice" should become "ehay isway icenay" but my output is like this "e is nicehay". thank you so much for your help, i really appreciate it. here is my code
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
String yourSentence="";
String[] tokens;
do
{
System.out.print("Enter your words here: ");
yourSentence = input.nextLine();
if( yourSentence.startsWith("a") || yourSentence.startsWith("e") || yourSentence.startsWith("i") ||
yourSentence.startsWith("o") || yourSentence.startsWith("u"))
{
System.out.print(yourSentence+ "way");
}
else
{
System.out.print(yourSentence.substring(1)+yourSentence.substring(0,1)+"ay");
}
}
while(!yourSentence.equals("quit"));
}
}
share improve this question follow
asked
Nov 30 '14 at 23:34
NumbNuts
81●11 gold badge●22 silver badges●88 bronze badges
1
You need to split the sentence into words first, before you apply the Pig Latin rules. You should look into String#split. – irrelephant Nov 30 '14 at 23:38
add a comment
3 Answers
order by
up vote
0
down vote
accepted
import java.util.Scanner;
public class PigLatin {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
String yourSentence="";
do {
String[] words;
System.out.print("Enter your words here: ");
yourSentence = input.nextLine();
words = yourSentence.split(" ");
for (String word : words) {
if (word.startsWith("a") || word.startsWith("e") || word.startsWith("i") || word.startsWith("o") || word.startsWith("u"))
System.out.print(word + "way ");
else if (word.startsWith("sh") || word.startsWith("ch") || word.startsWith("th"))
System.out.print(word.substring(2)+word.substring(0,2)+"ay ");
else
System.out.print(word.substring(1)+word.substring(0,1)+"ay ");
}
System.out.println();
} while(!yourSentence.equals("quit"));
}
}
This also resolves multiple-letter/single-sound words, such as those beginning with "ch", "sh", and "th". If you want to be able to correctly handle punctuation, you'll have more work to do.
I also included a new line after each line of input to tidy up the output's presentation.
share improve this answer follow
answered
Nov 30 '14 at 23:52
jda
54●1212 bronze badges edited
Dec 1 '14 at 0:00
up vote
0
down vote
You can split the input into a String array and then loop through.
String[] words = yourSentence.split(" ");
for(int i = 0; i < words.length; i++){
//Do stuff
}
share improve this answer follow
answered
Nov 30 '14 at 23:41
Falk
26●22 silver badges●66 bronze badges
You can also do for(String word : words), much shorter. – Friso van Dijk Nov 30 '14 at 23:48
If you used a standard for loop you could modify the array and then do StringUtils.join(words, " ") at the end. Which might make things simpler. – Falk Nov 30 '14 at 23:51
add a comment
up vote
0
down vote
You were close. I split yourSentence in the array token. Then I intialised newSentence and used a for-loop to go through all tokens. Every item added to newSentence and in the end it prints this new sentence.
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
String yourSentence="";
String[] tokens;
do
{
System.out.println("Enter your words here: ");
yourSentence = input.nextLine();
tokens = yourSentence.split(" ");
String newSentence = "";
for(String token : tokens) {
if( token.startsWith("a") || token.startsWith("e") || token.startsWith("i") ||
token.startsWith("o") || token.startsWith("u"))
{
newSentence += token + "way ";
}
else
{
newSentence += token.substring(1) + token.substring(0,1) + "ay ";
}
}
System.out.println(newSentence);
}
while(!yourSentence.equals("quit"));
}
share improve this answer follow
answered
Nov 30 '14 at 23:47
Friso van Dijk
669●33 silver badges●1414 bronze badges
Your Answer
Body
Add picture
Log in
OR
Name
By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy
meta chat tour help blog privacy policy legal contact us full site
2020 Stack Exchange, Inc. user contributions under cc by-sa
Answer:
I'm writing program called piglatin. The program will keep running until user type quit. Any words start with a vowel, the end of the word must add "way". If the word start with a consonant, then I have to move the consonant to the end of the word and add word "ay". The only problem i have is how to make my program to be able to change all the words individually instead of based on the first word of a sentence. For example "he is nice" should become "ehay isway icenay" but my output is like this "e is nicehay". thank you so much for your help, i really appreciate it. here is my code
import java.util.Scanner;
public class PigLatin
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in );
String yourSentence="";
String[] tokens;
do
{
System.out.print("Enter your words here: ");
yourSentence = input.nextLine();
if( yourSentence.startsWith("a") || yourSentence.startsWith("e") || yourSentence.startsWith("i") ||
yourSentence.startsWith("o") || yourSentence.startsWith("u"))
{
System.out.print(yourSentence+ "way");
}