Eva Frankovic
Eva Frankovic

Reputation: 31

What am I doing wrong with my code? Translating into PigLatin and cant figure out how to print returned value

import java.util.*;
public class Project3 {
    public static void main (String[] args) {

    ArrayList<String> translationOfPigLatin = new ArrayList<String>();
    Scanner Scanner = new Scanner(System.in);
    translationOfPigLatin.add(Scanner.next());
    StringBuffer sb = new StringBuffer();
      for(int i = 0; i < pigLatin.length; i++) {
        System.out.println(pigLatin[i]);

      } 
    }
    public static String translateToPigLatin(String pigLatin){
    int num = 0;
    String translationToPigLatin = pigLatin;

    if (pigLatin.indexOf('a') == num){
        System.out.print(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('e') == num){
        System.out.println(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('i') == num){
       System.out.println(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('o') == num){
        System.out.println(pigLatin + "ay");
            return pigLatin;
       } else if (pigLatin.indexOf('u') == num){
        System.out.println(pigLatin + "ay");
            return pigLatin;
       } else {
        char firstLetter = pigLatin.charAt(0); 
        pigLatin = pigLatin.substring(1); 
        System.out.println(pigLatin + firstLetter + "ay");
            return pigLatin; 
        }
    }
}

I dont know how to return the pigLatin value to be able to print it out. The instructions for my code was: Your program must take in a sentence (of any length) as command line arguments. Your program must contain a method named translateToPigLatin() that takes in an array of Strings as the argument (the input), and returns a single String (the Pig Latin translation). Each word in the sentence must be translated to Pig Latin using the rules above. You must print the sentence’s Pig Latin translation back out on one line. You can ignore punctuation and capitalization, and assume all test input will only consist of lowercase letters and spaces.

Upvotes: 0

Views: 259

Answers (2)

Nirro
Nirro

Reputation: 769

Let's take this step by step. Let's forget about everything else for a moment and center on the translateToPigLatin function.

We need to create a function that takes an String and outputs another, which will be its Pig Latin equivalent. You almost nailed that down. Here is such function with just a couple adjustments over your original code:

    // Let's write a function that takes an input string and outputs pigLatin translation
    public static String translateToPigLatin(String pigLatin) {
        int num = 0;
        // This translationToPigLatin variable ONLY EXISTS between the start and end {} of this function.
        // You are not really using it inside this function, so let's forget about it.
        // String translationToPigLatin = pigLatin;

        // Instead of writing output now. Let the function just do the transform and we'll do output later on.
        // I've commented out all the System.out.print
        if (pigLatin.indexOf('a') == num) {
            //System.out.print(pigLatin + "ay");

            // Whenever we do a return statement, we need to put the value that we want the function to return.
            // In your code, you called System.out.print with the appropriate value but returned just the pigLatin
            // variable which would contain the original word. We need to return the translated value instead. 
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('e') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('i') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('o') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else if (pigLatin.indexOf('u') == num) {
            //System.out.println(pigLatin + "ay");
            return pigLatin + "ay";
        } else {
            char firstLetter = pigLatin.charAt(0);
            pigLatin = pigLatin.substring(1);
            //System.out.println(pigLatin + firstLetter + "ay");
            return pigLatin + firstLetter + "ay";
        }
    }

I don't know if this is the translation you need because you forgot to include the rules but this would be a working version of your translation function.

Now let's clear it up a bit.

First, the input variable name. Let's just call it input so we know what it stands for the whole time.

    public static String translateToPigLatin(String input) {

We can get rid of the int num = 0. If you need to check the start of the word it'd be more practical to do something like this:

        // Create a variable that holds the first character
        char firstLetter = input.charAt(0);

Now, since you have 5 cases in which your if would do the same thing, let's write a single condition for all of those.

        // In Java || is the logical OR operator. This condition will be true if the first predicate is true OR
        // the second OR the third...
        // In other words. If any of these comparisons are true. The condition will be true.
        if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u') {
            // And then we'll do this.
            return input + "ay";
        }

Since we already have the first letter we can write the else such as:

        else {
            // A variable with all but the first letter
            String noFirstLetter = input.substring(1);
            // And return the piglatin translation
            return noFirstLetter + firstLetter + "ay";
        }

Putting it all together looks like this:

    public static String translateToPigLatin(String input) {
        char firstLetter = input.charAt(0);

        if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u') {
            return input + "ay";
        } else {
            String noFirstLetter = input.substring(1);
            return noFirstLetter + firstLetter + "ay";
        }
    }

The only thing that does is transform the information contained in input somehow and return the result of that transformation. Now that we have that let's forget everythin about how it works and consider it a blackbox of a kind. We'll just know that if you do String output = translateToPigLatin("something") we'll get omethingsay as the output.

This is a way we could use that to make what your exercise describes.

    public static void main(String[] args) {
        // We need to process the console input. The way you originally used is indeed a good way to do that.
        // Notice the lowercase scanner. In Java, by convention, variables are named starting lowercase.
        Scanner scanner = new Scanner(System.in);

        // Every scanner.next call we'll return an input word. Knowing that we just need to accumulate those.
        // A List seems appropriate for that task.
        List<String> list = new ArrayList<>();

        // As we don't know how many input strings we are receiving we'll just have to rely on scanner's hasNext method
        // to known when we are done. When there's nothing more to read, hasNext will return false, ending our loop.
        while(scanner.hasNext()) {
            String word = scanner.next();
            // We'll immediately translate every received word using our translation function
            String translation = translateToPigLatin(word);
            // Then store the translation and repeat
            list.add(translation);
        }

        // When we reach the end of the words we then proceed to write them in the console in one line, separated by
        // spaces. We can use String.join to generate a big String containing every word on list, separated by spaces.
        String result = String.join(" ", list);
        // And print it out
        System.out.println(result);
    }

After writing a sequence manually, you need to use Ctrl+D if in Linux or Ctrl+Z and then in Windows to tell the program you are done. I hope this was useful. If it solves your problem, please don't forget to mark my answer as solution so it can help more people in the future.

Upvotes: 0

vivek kumar
vivek kumar

Reputation: 25

First of all you need to know that one can not use reserved words as variable names. So, first change the code as follows:

Scanner scanner = new Scanner(System.in);// not as Scanner Scanner.

and then in the main function you don't have any variable named pigLatin and hence you will have no access to it. And as per I can understand your question statement you need to have array of strings as parameter in the translateToPigLatin method. For returning pigLatin array you can save the each string's pigLatin into a variable and assemble them into an array variable and return that array variable.

Upvotes: -1

Related Questions