473,396 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Help with PigLatin Program (new to java)

Hey guys,

I'm a college student in my first programming class and I'm having trouble with an assignment. The requirements to the assignment can be found here: http://cs.millersville.edu/~zoppetti/161/Lab8.html

Anyways, I'm using scanner to scan a string and tokenize it into words. However, I need the user's input to be returned without any spaces. Then I need to convert the into piglatin. I appreciate any advice anyone can give me.

Here's my code:

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class PigLatin {
  4.  
  5. public static void main(String[] args) {
  6. printIntro();
  7.  
  8.  
  9. Scanner input = new Scanner(System.in);
  10. System.out.print("Please enter an English phrase or sentence ending with a period:");
  11. String sentence = input.nextLine();
  12. System.out.println("\"" +sentence+ "\"" + " ");
  13. Scanner word = new Scanner(sentence);
  14. System.out.println(" " + "in" + " " + "Pig" + " " + "Latin" + " "+ "is ");
  15. System.out.print("\"");
  16.  
  17. while (word.hasNext()) {
  18. String pigLatin = word.next();
  19. System.out.print(convertPigLatinWord(pigLatin));
  20. }
  21.  
  22.  
  23.  
  24.  
  25. }
  26.  
  27. public static void printIntro() {
  28. System.out.println("\t****"+ "Welcome to Program Translate to Pig Latin!" + "****");
  29. System.out.println("\t***"+ "This Program will translate an English Sentence" + "***");
  30. System.out.println("\t\t**" + "into Pig Latin." + "**");
  31. System.out.println();
  32.  
  33. }
  34.  
  35. public static String convertPigLatinWord(String englishWord) {
  36. int length = englishWord.length();
  37.  
  38. if (englishWord.charAt(length - 1) == '.'
  39. || englishWord.charAt(length - 1) == '!'
  40. || englishWord.charAt(length - 1) == '?') {
  41.  
  42. char ch = englishWord.charAt(0);
  43. String rest = englishWord.substring(1, length - 1);
  44. return (rest + "-" + ch + "ay" + englishWord.charAt(length - 1) + "\""+" ");
  45.  
  46. } else if ((englishWord.charAt(0) == 't' || englishWord.charAt(0) == 'T')
  47. && englishWord.charAt(1) == 'h') {
  48. return (convertTh(englishWord)+" ");
  49.  
  50. } else if (isVowel(englishWord.charAt(0))) {
  51. return (englishWord + "-way"+" ");
  52.  
  53. } else {
  54. char ch = englishWord.charAt(0);
  55. String rest = englishWord.substring(1);
  56. return (rest + "-" + ch + "ay"+" ");
  57.  
  58. }
  59.  
  60. }
  61.  
  62. public static String convertTh(String tWord) {
  63. String th = tWord.substring(0,2);
  64. String rest = tWord.substring(2);
  65. return (rest + "-" + th+ "ay");
  66.  
  67. }
  68.  
  69. public static String convertPigLatinPhrase(String englishPhrase){
  70.  
  71. Scanner word= new Scanner(englishPhrase);
  72. String nextWord = word.next();
  73. int space=nextWord.indexOf(" ");
  74. String nospace= nextWord.substring(space+1,englishPhrase.length()) ;
  75.  
  76. return(nospace+" ");
  77.  
  78.  
  79. }
  80.  
  81.  
  82.  
  83. public static boolean isVowel(char c) {
  84. if (c == 'a' || c == 'A') {
  85. return (true);
  86.  
  87. } else if (c == 'e' || c == 'E') {
  88. return (true);
  89.  
  90. } else if (c == 'i' || c == 'I') {
  91. return (true);
  92. } else if (c == 'o' || c == 'O') {
  93. return (true);
  94.  
  95. } else if (c == 'u' || c == 'U') {
  96. return (true);
  97.  
  98. } else {
  99. return (false);
  100.  
  101. }
  102.  
  103. }
  104.  
  105. }
Nov 2 '08 #1
7 18321
JosAH
11,448 Expert 8TB
I'm sorry I don't understand your question; neither do I understand why you're doing this:

Expand|Select|Wrap|Line Numbers
  1. System.out.println(" " + "in" + " " + "Pig" + " " + "Latin" + " "+ "is ");
  2.  
while you could've simply done this:

Expand|Select|Wrap|Line Numbers
  1. System.out.println(" in Pig Latin is ");
  2.  
The Scanner.next() method reads a Strings without spaces in it but that is all
I can sensibly say now.

kind regards,

Jos
Nov 2 '08 #2
I need to break the user input into words, by using scanner. I need to make it so that if the user input multiple spaces, such as, " There was a black cat." will be returned as "There was a black cat." I need to return the inputted sentence to pig Latin using the convertPigLatinPhrase method. I'm really stumped on how to fix my program; I've been playing around with it for several hours the last few days and can't figure it out.

I really appreciate the help, and remember I'm a newbie to java so I apologize for this.

Thanks, I hope you guys can help.
Nov 2 '08 #3
JosAH
11,448 Expert 8TB
I need to break the user input into words, by using scanner. I need to make it so that if the user input multiple spaces, such as, " There was a black cat." will be returned as "There was a black cat." I need to return the inputted sentence to pig Latin using the convertPigLatinPhrase method. I'm really stumped on how to fix my program; I've been playing around with it for several hours the last few days and can't figure it out.

I really appreciate the help, and remember I'm a newbie to java so I apologize for this.

Thanks, I hope you guys can help.
If you want to replace multiple adjacent spaces to a single space use the
String.replaceAll() method like this:

Expand|Select|Wrap|Line Numbers
  1. String s= "There      was     a  black           cat";
  2. s=s.replaceAll(" +", " ");
  3.  
This method uses regular expressions; the first argument reads: one or more spaces
and the second argument simply is a single space.

kind regards,

Jos

ps. can you briefly describe the rules for the pig Latin transformation?
Nov 3 '08 #4
The replace all method works fine for replacing the space between words such as: "there was a cat" becomes "there was a cat." However, this method fails when there is a space at the beginning of the sentence. If the user inputted something like " there was a cat." The output would then be " there was a cat." which is fine except for the space at the beginning. How would I get rid of this?

The rules for converting to Pig Latin are as follows.

1. Generally, convert a word by removing the first letter. Append to the end of the word a hyphen, followed by the first letter that was removed, followed by "ay". For example, "robot" becomes "obot-ray" and "hello" becomes "ello-hay".
2. If the word begins with a vowel, simply append a hyphen followed by "way". For example, "example" becomes "example-way" and "all" becomes "all-way".
3. If the word begins with "th", move the "th" to the end, not just the first letter ("t"), and append "ay". For example, "there" becomes "ere-thay".

I'm trying to clean up my main method, but i'm stuck on how to do so. When I attempt to move my while loop from main into convertPigLatinPhrase, I can't get the method to return when the return statement is in the while loop. Is this possible or how would I set up my method (convertPigLatinPhrase) so that it would break the user's sentence into strings with only one space between the words as well as no spaces in the beginning or end of the sentence.

I've tried to move some of my code from "main" into my convertPLPhrase method, and make a method call to convertPLPhrase in main passing in a the user sentence as a string, but I got an error message. I'm assuming that's the way it's supposed to be done. I've spent a lot of time on this program, which I first thought was simple has now turned into a nightmare. Hopefully I can get it cleaned up by Wednesday.

Thanks again. Here's the updated code as well.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2.  
  3. public class Pig {
  4.  
  5.     public static void main(String[] args) {
  6.         printIntro();
  7.  
  8.         Scanner input = new Scanner(System.in);
  9.         System.out.print("Please enter an English phrase or sentence ending with a period:");
  10.         String sentence = input.nextLine();
  11.         sentence=sentence.replaceAll(" +", " ");
  12.         System.out.println("\"" + sentence + "\"" + " ");
  13.         Scanner word = new Scanner(sentence);
  14.         System.out.println("  " + "in" + " " + "Pig" + " " + "Latin" + " "+ "is ");
  15.         System.out.print("\"");
  16.  
  17.         while (word.hasNext()) {
  18.             String pigLatin = word.next();
  19.             System.out.print(convertPigLatinPhrase(pigLatin));
  20.         }
  21.  
  22.     }
  23.  
  24.     public static void printIntro() {
  25.         System.out.println("\t****"+ "Welcome to Program Translate to Pig Latin!" + "****");
  26.         System.out.println("\t***"+ "This Program will translate an English Sentence" + "***");
  27.         System.out.println("\t\t**" + "into Pig Latin." + "**");
  28.         System.out.println();
  29.  
  30.     }
  31.  
  32.     public static String convertPigLatinWord(String englishWord) {
  33.         int length = englishWord.length();
  34.  
  35.         if (englishWord.charAt(length - 1) == '.'
  36.                 || englishWord.charAt(length - 1) == '!'
  37.                 || englishWord.charAt(length - 1) == '?') {
  38.  
  39.             char ch = englishWord.charAt(0);
  40.             String rest = englishWord.substring(1, length - 1);
  41.             return (rest + "-" + ch + "ay" + englishWord.charAt(length - 1) + "\"");
  42.  
  43.         } else if ((englishWord.charAt(0) == 't' || englishWord.charAt(0) == 'T')
  44.                 && englishWord.charAt(1) == 'h') {
  45.             return (convertTh(englishWord));
  46.  
  47.         } else if (isVowel(englishWord.charAt(0))) {
  48.             return (englishWord + "-way");
  49.  
  50.         } else {
  51.             char ch = englishWord.charAt(0);
  52.             String rest = englishWord.substring(1);
  53.             return (rest + "-" + ch + "ay");
  54.  
  55.         }
  56.  
  57.     }
  58.  
  59.     public static String convertTh(String tWord) {
  60.         char t = tWord.charAt(0);
  61.         char h = tWord.charAt(1);
  62.         String rest = tWord.substring(2);
  63.         return (rest + "-" + t + h + "ay");
  64.  
  65.     }
  66.  
  67.     public static String convertPigLatinPhrase(String englishPhrase) {
  68.         String word = convertPigLatinWord(englishPhrase);
  69.         return (word + " ");
  70.  
  71.     }
  72.  
  73.     public static boolean isVowel(char c) {
  74.         if (c == 'a' || c == 'A') {
  75.             return (true);
  76.  
  77.         } else if (c == 'e' || c == 'E') {
  78.             return (true);
  79.  
  80.         } else if (c == 'i' || c == 'I') {
  81.             return (true);
  82.         } else if (c == 'o' || c == 'O') {
  83.             return (true);
  84.  
  85.         } else if (c == 'u' || c == 'U') {
  86.             return (true);
  87.  
  88.         } else {
  89.             return (false);
  90.  
  91.         }
  92.  
  93.     }
  94.  
  95. }
Nov 3 '08 #5
JosAH
11,448 Expert 8TB
You're complicating matters far too much; a Scanner.next() method returns a
String and it skips any leading space from its input; one problem solved. For
a start have a look at this:

Expand|Select|Wrap|Line Numbers
  1. private static String pigLatinify(String word) {
  2.  
  3.     return word;
  4. }
  5.  
  6. public static void main(String args[]) {
  7.  
  8.         Scanner s= new Scanner(System.in);
  9.  
  10.         while (s.hasNext())
  11.             System.out.println("'"+pigLatinify(s.next())+"'");
  12. }
  13.  
As you can see the 'pigLatinify()' method doesn't do anything yet but you can
exercise the main() method and see that just the words are returned without
any spaces. That simplifies the main() method quite a bit; another problem solved.

Now you can concentrate on the actual 'pigLatinification' of a single word. If you
read the API documentation of the String class you can find a handy method
that can be used for the vowel check:

Expand|Select|Wrap|Line Numbers
  1. boolean isVowel(char c) {
  2.         returnn "aAeEiIoOuU".indexOf(c) >= 0;
  3. }
  4.  
The String.startsWith() method can check whether or not a String starts with 'th'
so we're basically done: if the above two tests fail we simply move the first
character to the new suffix: -<x>ay where <x> was the first character; your turn now.

kind regards,

Jos
Nov 3 '08 #6
okay, I have the main looking like this now:
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) {
  2.         printIntro();
  3.  
  4.         Scanner input = new Scanner(System.in);
  5.         System.out.print("Please enter an English phrase or sentence ending with a period:");
  6.  
  7.  
  8.         while (input.hasNext()){
  9.  
  10.             System.out.print(input.next()+" ");
  11.  
  12.         }
  13.  
  14.         System.out.println("  " + "in Pig Latin is ");
  15.         System.out.print("\"");
  16.     }
  17.  
The problem is, nothing executes after the while loop. I get the user input formatted correctly, but I want to now convert that to PigLatin, but I can't because the while loop stops the program. How do I proceed from here?
Nov 3 '08 #7
JosAH
11,448 Expert 8TB
The problem is, nothing executes after the while loop. I get the user input formatted correctly, but I want to now convert that to PigLatin, but I can't because the while loop stops the program. How do I proceed from here?
All you do now is read every word, print it and forget all about it. You either
have to pigLatinify each word, store it and print them all afterwards; or you store
the original words, pigLatinify them all when the while loop has stopped and
print them. Have a look at the ArrayList<String> class.

kind regards,

Jos
Nov 4 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: djtosh | last post by:
hi i dont know if this is the right place to be asking this but im trying to create a small program in java that can calculate the area of a square and then of multiple squares if the user enters the...
3
by: khajeddin | last post by:
hello every one: does any software exist that can convert a C or VB program to Java program?and does this software work as well as you write that program in Java itself? if so please tell me .
2
by: wing ver ka gundam | last post by:
THanks~ this is the example and my quote are below Write a program in JAVA that allows the user to input 5 ints. The program will then output these five values in reverse order. The program will...
2
by: ksp22pma | last post by:
Can't read the input and output of C program in Java. public class chkFinal { public static void main(String args) { try{ Runtime rt = Runtime.getRuntime(); Process proc = rt.exec("cmd /c...
2
by: shikoh | last post by:
Hello, I really need your help.Well,i have been given this programming project where am supposed to create a core program in java which recieves input from other programs for manupulation.The core...
0
by: Siyodia | last post by:
This is a java program which i need to run facing compilation error Its consuming a third party web service method I have the supported files(folder) which contain necessary class files...
3
sreekandank
by: sreekandank | last post by:
I want to implement POP3 program in java....I have written the code like... import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; public class POP3Client {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.