My code wont work for some reason. The .length in my for loop (words.length()) is marked as an error for some reason.I really dont know why it is coming up as an error. Here is the code:
import java.util.Scanner;
public class Piglatin {
/**
* @param args the command line arguments
*/
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( );
}
}
Aucun commentaire:
Enregistrer un commentaire