ConvertPhrase String index вне диапазона: 0 - PullRequest
0 голосов
/ 05 ноября 2019

У меня есть код, который готов, но автогрейдер выдает ошибку:

код:

import java.util.Scanner;

public class PigLatin {

    public static void main(String[] args) {  // main method

        Scanner scan = new Scanner(System.in);
        String englishPhrase = readPhrase(scan); // this method will call readPhrase to get the input
        String pigLatinPhrase = convertPhrase(englishPhrase); // this method will call converPhrase to covert to pig
                                                                // latin
        printResult(englishPhrase, pigLatinPhrase); // this method will call the printResult function to print result

    }

    public static String readPhrase(Scanner console) // this method will take an input of English phrase from the user
    {
        String phrase0;
        System.out.println("This program will convert an English phrase into Pig Latin.\n");
        String phrase;
        System.out.print("Please enter a phrase ==> ");
        phrase = console.nextLine();
        System.out.println();
        return phrase;
    }

    public static String convertPhrase(String englishPhrase) // this method will convert the English to Pig Latin phrase
    {
        String pigLatinPhrase = "";
        String words[] = englishPhrase.split(" "); // this method grabs the words from the phrase and split them with a
                                                    // space between them
        for (int i = 0; i < words.length; i++) // this is the loop that convert each word to pig Latin and add it to
                                                // result string
        {
            if (i < words.length - 1)
                pigLatinPhrase += convertWord(words[i]) + " ";
        }

        if (words.length > 0) // this if statement will convert the last word to Pig Latin
        {
            pigLatinPhrase += convertWord(words[words.length - 1]);
        }

        return pigLatinPhrase;
    }

    public static String convertWord(String englishWord) // this method convert a word to Pig Latin and return it
    {

            String pigLatinWord;
            if (isVowel(englishWord.charAt(0)))
                pigLatinWord = englishWord + "-" + "way";
            else if (englishWord.startsWith("th") || englishWord.startsWith("Th") || englishWord.startsWith("tH")
                    || englishWord.startsWith("TH")) {
                pigLatinWord = englishWord.substring(2) + "-" + englishWord.substring(0, 2) + "ay";
            } else
                pigLatinWord = englishWord.substring(1) + "-" + englishWord.charAt(0) + "ay";
            return pigLatinWord;
        }


    public static boolean isVowel(char c) // this method will return true if the vowl is "c" false otherwise
    {

        return ("aeiouAEIOU".contains(c + "")); // this will return true if c is a,e,i,o or u in any upper or smaller
                                                // case
    }

    public static void printResult(String englishPhrase, String pigLatinPhrase) // this method will print the result of
                                                                                // translation
    {
        System.out.println("\"" + englishPhrase + "\"" + "\n  in Pig Latin is\n" + "\"" + pigLatinPhrase + "\"");
    }

}

сообщение автогрейдера:

-20: testReadPhrase(PigLatinTest)

Description: Prompt is incorrect expected:<[Please enter a phrase
 ==> ]> but was:<[This program will convert an English phrase into Pig Latin.

Please enter a phrase ==>  ]>

-30: testConvertPhrase(PigLatinTest)

Description: String index out of range: 0
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...