found this code on the internet, it's missing the while loop logic "while(i....)" for some reason and though I found other working solutions for the PigLatin* problem, I really want to understand how this one is working.
*PigLatin problem: take a sentence, take the first letter from each word and place it at the end of the same word, then suffix "ay". So "I am confused" becomes "Iay maay onfusedcay".
Here is the code:
import java.util.*;
public class PigLatin {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an English sentence: ");
String sentence = input.nextLine();
System.out.println("Original sentence: "+sentence);
System.out.println("PigLatin conversion: "+convert(sentence));
}
private static String convert (String sentence) {
String []words = sentence.split(" ");
int i = 0;
String pigLatin = "";
while(i ){ //MISSING CODE
pigLatin+=words[i].substring(1,words[i].length())+words[i].charAt(0)+"ay"+" ";
i++;
}
return pigLatin;
}
}
Thank you.
PS: I basically found the "convert" method on the internet and wrote the rest of the code, tried a few things but could not get the while loop to work.
The loop appears to be iterating the words array. So it should just be something like
while(i < words.length) {
pigLatin += words[i].substring(1, words[i].length())
+ words[i].charAt(0) + "ay ";
i++;
}
import java.util.Scanner;
public class PigLatin {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter an English sentence: ");
String sentence = input.nextLine();
System.out.println("Original sentence: "+sentence);
System.out.println("PigLatin conversion: "+convert(sentence));
}
private static String convert (String sentence) {
String []words = sentence.split(" ");
int i = 0;
String pigLatin = "";
while(i<words.length){ //CORRECTION CODE
pigLatin+=words[i].substring(1,words[i].length())+words[i].charAt(0)+"ay"+" ";
i++;
}
return pigLatin;
}
}