PDA

View Full Version : Pig Latin Translator


NoobCoder8769
01-10-2010, 12:26 AM
Ok I was writing a pig latin translator in java. I thought I knew what I was doing but when I compiled it, I had a million errors. I'm absolutely lost now :(.
(i didnt totally finish either :()

What I was trying to do:

-get input from user
-put in string
-split string at every space and put each word into array
-get each item in this array and translate to pig latin and then concatonate onto a different array
-print out final array

PLEASE help me out here :(...


import java.util.Scanner;
public class PigLatinTranslator
{
String[] output;

public static String[] inputa(String[] userin1)
{
for (int i=0; i<userin1.length();i++)
{
String word = output[i]; //get first word
String firstchar = userin1.substring(0,1); //get first char

if (firstchar.equalsIgnoreCase("A") || firstchar.equalsIgnoreCase("E") || //create word
firstchar.equalsIgnoreCase("I") || firstchar.equalsIgnoreCase("O") ||
firstchar.equalsIgnoreCase("U"))
{
String finalword = word.substring(1);
finalword += (firstchar+"ay");
output += finalword;
}
}
}




public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("This is a Pig Latin Translator");
System.out.println("Please input what you would like translated: ");
String userinput = scanner.next();
String[] userinputb[] = userinput(" ");
String[] translation[] = new Array[userinputb];
System.out.println("");
System.out.println("");
System.out.println("Translated: ");
System.out.println("");

}
}

cs_student
01-10-2010, 03:26 AM
You should really get yourself familiar with the String (http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html) class. It has lot's of methods which will help you with this, as well as later, endeavors.

First, you forgot to specify String[] output to be public or private. You also forgot to instantiate it. Note you can't use non-static class members in static methods.

String word = output[i]; //get first word


Why are you trying to get the word from the output array? You haven't initialized the array, nor have populated the array with any data. You should probably be getting your words from the userin1 array which is passed in as a parameter to the method.

Try and clean up your code and post back if you need further help. If you continue to get compiler problems, post your compilers error output. You may have more errors, but I'm not going to go looking for them when you could provide me with what they are.


Regards,


cs_student

NoobCoder8769
01-10-2010, 10:43 PM
Ok I cleaned up my code. This version runs, but it's not working properly. I tried to split the string at every space in the users input. then i tried putting all that into an array. To test if it worked properly, I printed out the array. It printed out only the first word.



import java.util.Scanner;
public class PigLatinTranslator
{

public static String[] inputa(String[] userin1)
{
for (int i=0; i<userin1.length;i++)
{
String word = userin1[i]; //get first word
String firstchar = word.substring(0,1); //get first char

if (firstchar.equalsIgnoreCase("A") || firstchar.equalsIgnoreCase("E") || //create word
firstchar.equalsIgnoreCase("I") || firstchar.equalsIgnoreCase("O") ||
firstchar.equalsIgnoreCase("U"))
{
String finalword = word.substring(1);
finalword += (firstchar+"ay");
// output += finalword;
}
}
return userin1;
}




public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("This is a Pig Latin Translator");
System.out.println("Please input what you would like translated: ");
String userinput = scanner.next();

String[] userinputb = userinput.split(" ");
String[] translation = new String[userinputb.length];

for(int a=0;a<userinputb.length;a++)
{
translation[a] = userinputb[a];
}

System.out.println("");
System.out.println("");
System.out.println("Translated: ");
System.out.println("");
for (int i=0; i<translation.length;i++)
{
System.out.println(translation[i]);
}

}
}

cs_student
01-11-2010, 12:21 AM
The Scanner (http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html) class would be another good one to get acquainted with.
The reason you only get the first word is because the next() method only returns one word. You can create a loop checking if there is more input and retrieving the input with next(). You could also take a look at the nextLine() method which will return the entire line that is typed as a String. You could then split that into an array of words. I suggest you do the latter.

Also, why are you copying userinputb to translation? Why not just use userinputb?

You should also consider using a better naming convention. userInputB might be easier to read.

You should have a look at the Java Coding Conventions (http://java.sun.com/docs/codeconv/).


Regards,


cs_student

Old Pedant
01-11-2010, 07:05 PM
Ummm...excuse me, but you wrote that function to do the Pig Latin translation...and then you NEVER NEVER call it!!!!

How do you expect it to do its work if you don't ask it to do so??