문자열을 취하고이를 이와 같이 변환하는 약간의 코드를 작성하려고합니다. 1. 첫 글자를 가져 와서 모든 단어에 대해 단어 끝 부분에 넣으십시오. 2. 첫 번째 모음을 찾아서 'b'와 모음을 다시 입력하십시오. 3. 마지막 글자를 제외하고는 # 2와 동일하게하십시오 모음내 ArrayList (s)는 단지 숫자를 인쇄하고 있습니다
필자는 일종의 가깝지만 내 출력은 모두 숫자라고 생각합니다. 그것은 그것이 저장된 곳의 주소처럼 보이지 않습니다.
return 문에 배열 목록을 인쇄 할 때 동일한 문제가 발생할 수 있으므로 다른 사람들에게 도움이되기를 바랍니다. 그건 그렇고, 그것은 거대한 코드 블록입니다 ... 죄송합니다. 내가 그랬던 유일한 이유는 두 클래스를 여기에 둘 필요가 없었기 때문입니다. ..
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();
}
}
너무 많은 코드 (이미 나쁜 점)를 붙여 넣을 때 우리는 어디를 봐야하는지에 대한 자세한 정보를 제공해야합니다. –
코드를 디버그하면 곧 문제가 발생할 것입니다. 약속 할께. – Maroun
나는 당신의 질문에 대한 답은 당신이'char's concatente에 노력하고 있다고 생각합니다. 그것이 당신이 숫자를 얻는 이유입니다. 내 대답을 확인하십시오. –