0

I have code where one method takes user input and converts a single word into pig latin. But, I need to convert a whole phrase into pig latin and I do not know how to convert the whole nextline() statement into pig latin as opposed to just one word.

I have thought of using a for loop and other things I just do not understand how I would break the user input into single words

import java.util.*;
public class PigLatin {
public static void main(String[] args) {
    System.out.println(" This Program creates  pig latin out of the user inputted word");
    System.out.println();
 Scanner console = new Scanner(System.in);
    System.out.println("Type a word in:");
    String userWord = console.nextLine();
convertWord( userWord);


}

public static void convertWord(String word) {
    word = word.toLowerCase();
    char first = word.charAt(0);
    //String middle = word.substring(2);
     String partWord = word.substring(1,word.length());
    if( first == 'a' || first == 'e' ||first == 'i' ||first == 'o' ||first == 'u'  ) {
        System.out.println(word + "-way");

    } else {
        //char last = word.charAt(word.length() - 1);
        System.out.println(partWord + "-" + word.charAt(0) + "ay");

    }
}
}

I expect to input any phrase and if it will put the equivilent but each word is in pig latin. If the word starts with a vowel it will append the word plus -way. or else it will append the word followed by the first letter and then it will output (ay). I need all the words in the phrase to do this .

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
  • `String::split` – GBlodgett Apr 17 '19 at 23:42
  • I am aware I need to split it but I still have no clue as to where the above comment (String:: split) would go in my program? – TylerCodes Apr 18 '19 at 00:27
  • Hi Tyler! [Here](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) and [here](https://www.geeksforgeeks.org/split-string-java-examples/) are some good resources on `String::split`! – GBlodgett Apr 18 '19 at 00:29

2 Answers2

0

Keeping your current structor as to not make things to complicated, here is a quick solution. Your biggest problem with your example is you're not looking at each word.

input: the quick brown fox jumps over the lazy dog

output: he-tay uick-qay rown-bay ox-fay umps-jay over-way he-tay azy-lay og-day

private static void convertWord(String sentence) {

    // Split the sentence into words so we can deal with one word at a time.
    final String[] words = sentence.split("\\w+");

    // Create a string builder to store the results of each iteration.
    final StringBuilder result = new StringBuilder();

    // Loop over each word in the sentence and decide what to do with it.
    for (final String word : words) {
        char first = word.charAt(0);

        String partWord = word.substring(1);

        if (first == 'a' || first == 'e' || first == 'i' || first == 'o' || first == 'u') {
            result.append(word).append("-way").append(" ");
        } else {
            result.append(partWord).append("-").append(word.charAt(0)).append("ay").append(" ");
        }
    }

    // Output the result
    System.out.println(result.toString());
}

I've not changed your 'core' logic so there may still be mistakes with it, but this should get you on your way.

One improvement you could make would be to make sure you've got a letter/word at each point as a null pointer is pretty easy to produce.

Chris
  • 3,437
  • 6
  • 40
  • 73
0

Here is quick fix for you. Kindly refer it.

Input :

Enter sentence: I love java

Output :

I-ay ove-lay ava-jay

import java.io.*;

public class PigLatin {

  private static BufferedReader buf = new BufferedReader(
      new InputStreamReader(System.in));
  public static void main(String[] args) throws IOException {
    System.out.print("Enter sentence: ");
    String english = getString();
    String latin = pigLatin(english);
    System.out.println(latin);
  }
  private static String pigLatin(String s) {
    String latin = "";
    int i = 0;
    while (i<s.length()) {
      while (i<s.length() && !isLetter(s.charAt(i))) {
        latin = latin + s.charAt(i);
        i++;
      }
      if (i>=s.length()) break;
      int begin = i;
      while (i<s.length() && isLetter(s.charAt(i))) {
        i++;
      }
      int end = i;
      latin = latin + pigWord(s.substring(begin, end));
    }
    return latin;
  }
  private static boolean isLetter(char c) {
    return ( (c >='A' && c <='Z') || (c >='a' && c <='z') );
  }
  private static String pigWord(String word) {
    int split = firstVowel(word);
    return word.substring(split)+"-"+word.substring(0, split)+"ay";
  }
  private static int firstVowel(String word) {
    word = word.toLowerCase();
    for (int i=0; i<word.length(); i++)
      if (word.charAt(i)=='a' || word.charAt(i)=='e' ||
          word.charAt(i)=='i' || word.charAt(i)=='o' ||
          word.charAt(i)=='u')
        return i;
    return 0;
  }
  private static String getString() throws IOException {
    return buf.readLine();
  }
}

Hope this solution works.

Vimal
  • 411
  • 6
  • 28