0

我正在尝试编写一些代码,它需要一个字符串并像这样翻译它;1. 将第一个字母放在每个单词的词尾 2. 找到第一个元音并放一个“b”,然后再放元音 3. 除了最后一个元音外,与 #2 相同

我想我已经接近了,但我的输出都是数字。它甚至看起来不像它的存储地址。
我希望这对其他人有所帮助,因为他们可能在返回语句中打印数组列表时遇到同样的问题。顺便说一句,这是一个巨大的代码块......对不起。我这样做的唯一原因是我不必将两个类都放在这里。

这是代码:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Experiment {
    public static void main(String[] args){
        String pre = "For every minute you are angry you loose sixty seconds of happiness.";

        System.out.println(translate(pre));
    }

    public static String translate(String sentence){
        String[] sentenceArray = sentence.split(" ");
        List<String> sentenceList = new ArrayList<>();
        List<String> finalList = new ArrayList<>();
        String punctuation = getPunctuation(sentenceArray[sentenceArray.length - 1]);
//add all words but the last so i can take punctuation off it
        for (int i = 0; i < sentenceArray.length - 1; i ++){
            sentenceList.add(sentenceArray[i]);
        }
//take the first letter off each word and put at at the end of each word
        Arrays.asList(sentenceArray);
        for (String el : sentenceArray)
            sentenceList.add(firstToLast(el));
    //use the addFrontB method on each word     
        Arrays.asList(sentenceList);
        for (String la : sentenceList){
            finalList.add(addFrontB(la));
        }
//use the addBackB method on each word
        Arrays.asList(sentenceList);
        for (String le : sentenceList){
            finalList.add(addBackB(le));
        }
        return finalList + punctuation + "\n";
    }
//finds the last character of the last word which is punctuation
    public static String getPunctuation(String word){
        return word.charAt(word.length() - 1) + "";
    }
//takes the punctuation off
    public static String removePunctuation(String word){
        String newWord;
        newWord = word.substring(word.length(), word.length());
        return newWord;
    }
//puts the first letter at the end of the word
    public static String firstToLast(String word){
        char letter = word.charAt(0);
        String newWord = word.substring(1,word.length()) + letter;
        return newWord;
    }
//insterts a b and then the same vowel behind the first vowel
    public static String addFrontB(String word){
        StringBuilder finishedWord = new StringBuilder();

        for (int i = 0; i < word.length(); i ++){
            if (word.charAt(i) == 'a')
                finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
            else if (word.charAt(i) == 'e')
                finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
            else if (word.charAt(i) == 'i')
                finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
            else if (word.charAt(i) == 'o')
                finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
            else if (word.charAt(i) == 'u')
                finishedWord = finishedWord.append(word.charAt(i) + 'b' + word.charAt(i));
            }
        String newWord = finishedWord.toString();
        return newWord;

    }
//does the same as addFirstB but at the end of the word
    public static String addBackB(String word){
        StringBuilder finishedWord = new StringBuilder();
        finishedWord.append(word);
        finishedWord.reverse();
        for (int i = 0; i < word.length(); i ++){
            if (finishedWord.charAt(i) == 'a')
                finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
            else if (finishedWord.charAt(i) == 'e')
                finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
            else if (finishedWord.charAt(i) == 'i')
                finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
            else if (finishedWord.charAt(i) == 'o')
                finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
            else if (finishedWord.charAt(i) == 'u')
                finishedWord.append(finishedWord.charAt(i) + 'b').reverse();
        }       
        return finishedWord.toString();
    }
}
4

2 回答 2

0
    return finalList + punctuation + "\n";

你把它List当作一个字符串在这里..你需要做.toString()或类似的事情吗?

asList(T... a)
Returns a fixed-size list backed by the specified array.

您也没有使用返回值Arrays.asList

于 2013-10-16T06:31:33.700 回答
0

你的错误在这里:

return finalList + punctuation + "\n";

由于您使用加号运算符添加“\n”,因此 java 调用列表的 toString() 方法,该方法返回废话。您必须遍历列表并构建自己的字符串,最佳实践是使用 StringBuffers。

于 2013-10-16T06:37:05.397 回答