having problems with my pig latin translator (the translating portion)
Code Java:
import java.util.*;
public class PigLatinTranslator
{
public String translate (String sentence)
{
String result = "";
sentence = sentence.toLowerCase();
Scanner scan = new Scanner (sentence);
while (scan.hasNext())
{
result += translateWord (scan.next());
result += " ";
}
return result;
}
public boolean beginsWithVowel (String word)
{
String vowels = "aeiou";
char letter = word.charAt(0);
return (vowels.indexOf(letter) != -1);
}
public String translateWord (String word)
{
String result = "";
if (beginsWithVowel) [B][U]// says "cannot find symbol - variable beginswithVowel[/U][/B]
result = word + "yay";
else
if (beginsWithBlend (word))
result = word.substring(2) + word.substring(0,2) + "ay";
else
result = word.substring(1) + word.charAt(0) + "ay";
return result;
}
public boolean beginswithBlend (String word)
{
return ( word.startsWith ("bl") || word.startsWith ("sc") ||
word.startsWith ("br") || word.startsWith ("sh") ||
word.startsWith ("ch") || word.startsWith ("sk") ||
word.startsWith ("cl") || word.startsWith ("sl") ||
word.startsWith ("cr") || word.startsWith ("sn") ||
word.startsWith ("dr") || word.startsWith ("sm") ||
word.startsWith ("dw") || word.startsWith ("sp") ||
word.startsWith ("fl") || word.startsWith ("sq") ||
word.startsWith ("fr") || word.startsWith ("st") ||
word.startsWith ("gl") || word.startsWith ("sw") ||
word.startsWith ("gr") || word.startsWith ("th") ||
word.startsWith ("kl") || word.startsWith ("tr") ||
word.startsWith ("ph") || word.startsWith ("tw") ||
word.startsWith ("pl") || word.startsWith ("wh") ||
word.startsWith ("pr") || word.startsWith ("wr") );
}
}
Re: having problems with my pig latin translator (the translating portion)
Please read the Welcome Announcement, as this provides instructions for how to format your code to make it much more readable.
beginsWithVowel cannot be found because it is never defined, you must define the variable first (in this case as a boolean).
Re: having problems with my pig latin translator (the translating portion)
Thank you, but i'm not sure where i should define this as a boolean, & im not too sure how. (Yes i'm a noob).
Re: having problems with my pig latin translator (the translating portion)
Recommended reading: Java Variables and Methods
After a closer look, it seems you wish to call the method beginsWithVowel ?
Code :
if ( beginsWithVowel (word) )//
or
boolean beginsWithVowel = beginsWithVowel (word);