Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads a sentence as input and converts each word to \"Pig L

ID: 3568509 • Letter: W

Question

Write a program that reads a sentence as input and converts each word to "Pig Latin". In one version of Pig Latin, you convert a word by removing the first letter, placing that letter at the end of the word, and then appending "ay" to the word. Here is an example:

English: I SLEPT MOST OF THE NIGHT

Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY

The Code I Have:

import java.util.Scanner;

public class Hw6pr6
{
   public static void main(String[] args)
   {

       System.out.println("Please enter a phrase to convert into Pig Latin: ");

       Scanner keyboard = new Scanner( System.in );
       String phrase = keyboard.nextLine();

       String[] words = phrase.split(" ");

       for(int i = 0; i < words.length(); i++ )
       {
           char firstLetter = (words[i].charAt(0));
           if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u')
           {
               String vowel = words[i] +"way";
               System.out.print(vowel);
           }
           else
           {
               String start = words[i].substring(0,1);
               String end = words[i].substring(1,phrase.length());
               System.out.print(end + start + "ay");
           }
       }
       System.out.println( );
   }
}

Error Message:

Hw6pr6.java:16: error: cannot find symbol
for(int i = 0; i < words.length(); i++ )
^
symbol: method length()
location: variable words of type String[]

Explanation / Answer

import java.util.Scanner;

public class Hw6pr6
{
   public static void main(String[] args)
   {

       System.out.println("Please enter a phrase to convert into Pig Latin: ");

       Scanner keyboard = new Scanner( System.in );
       String phrase = keyboard.nextLine();

       String[] words = phrase.split(" ");

       for(int i = 0; i < words.length; i++ ) //Fix length() to length
       {
           char firstLetter = (words[i].charAt(0));
           if (firstLetter == 'a' || firstLetter == 'e' || firstLetter == 'i' || firstLetter == 'o' || firstLetter == 'u')
           {
               String vowel = words[i] +"way";
               System.out.print(vowel);
           }
           else
           {
               String start = words[i].substring(0,1);
               String end = words[i].substring(1,words[i].length()); //phrase changed to words[i]
               System.out.print(end + start + "ay");
           }
           System.out.print(" "); //space added
       }
       System.out.println( );
   }
}