I am very new to Java and am trying to create a program to translate a sentence into Pig Latin, moving the first letter of the word to the end and appending "y" at the end if the first letter was a vowel and "ay" at the end otherwise. I am required to use a queue for this. Currently my program is just terminating and I was wondering if anyone might be able to spot where I am going wrong or where to head next. Thanks!
import MyQueue.QueueList; import java.util.Scanner;
public class PigLatin {
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
QueueList word = new QueueList();
String message;
int index = 0;
char firstch;
System.out.print ("Enter an English sentence: ");
message = scan.nextLine();
System.out.println ("The equivalent Pig Latin sentence is: ");
firstch = Character.toLowerCase(message.charAt(0));
if (firstch != 'a' && firstch != 'e' && firstch != 'i' && firstch != 'o' && firstch != 'u'
&& firstch != ' ')
{
for (index = 1; index < message.length(); index++)
{
word.enqueue(new Character(message.charAt(index)));
}
word.enqueue(new Character (firstch));
word.enqueue(new Character ('a'));
word.enqueue(new Character ('y'));
word.enqueue(new Character(' '));
}
else if (firstch == 'a' || firstch == 'e' || firstch == 'i' || firstch == 'o' || firstch == 'u')
{
while (message.charAt(index) != ' ')
{
for (index = 1; index < message.length(); index++)
{
word.enqueue(new Character(message.charAt(index)));
}
}
word.enqueue((firstch));
word.enqueue( ('y'));
word.enqueue((' '));
}
else if (message.charAt(index) == ' ')
{
index++;
firstch = message.charAt(index);
}
while (!word.empty())
System.out.print(word.dequeue());
}
}
And here is the QueueList class from the MyQueue package:
// QueueList.java
//
// Class QueueList definition with composed List object.
package MyQueue;
public class QueueList {
private List a_queue;
public QueueList() {
a_queue = new List( "queue" );
}
public Object peek() throws EmptyListException {
if (a_queue.isEmpty())
return null;
else
return a_queue.getFirstObject();
}
public void print() {
a_queue.print();
}
public void enqueue(Object object) {
a_queue.insertAtBack(object);
}
public Object dequeue() throws EmptyListException {
return a_queue.removeFromFront();
}
public boolean empty() {
return a_queue.isEmpty();
}
}
You are not resetting index to 0 before entering your second while loop. Since index == message.length()
after the first loop ends, the second loop terminates immediately.
Edit: Re: Your latest update.
In your second loop you only dequeue the first message.length() characters from the word queue. If you've added -ay to the end you won't see it. Instead, loop on the length of the queue, not the length of the input message:
while (!word.empty())
System.out.print(word.dequeue());
I can spot numerous other issues with your logic (you are not removing the first letter and you are not processing individual words in the sentence), but the above changes should be enough to get you printing what's on the queue and send you on your way debugging.
来源:https://stackoverflow.com/questions/18093785/java-pig-latin-sentence-translator-using-queues