2013-10-16 3 views
0

문자열을 취하고이를 이와 같이 변환하는 약간의 코드를 작성하려고합니다. 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(); 
    } 
} 
+0

너무 많은 코드 (이미 나쁜 점)를 붙여 넣을 때 우리는 어디를 봐야하는지에 대한 자세한 정보를 제공해야합니다. –

+0

코드를 디버그하면 곧 문제가 발생할 것입니다. 약속 할께. – Maroun

+0

나는 당신의 질문에 대한 답은 당신이'char's concatente에 노력하고 있다고 생각합니다. 그것이 당신이 숫자를 얻는 이유입니다. 내 대답을 확인하십시오. –

답변

0

당신의 실수, 중 반환 값을한다 사용하지 않는 :

return finalList + punctuation + "\n"; 

때문에 추가 "\ n"더하기 연산자, 자바는 객체의 toString() 메서드를 사용하면 넌센스를 반환합니다. 목록을 반복하고 자신 만의 문자열을 작성해야합니다. 가장 좋은 방법은 StringBuffers를 사용하는 것입니다.

+0

StringBuffer에 익숙하지 않지만 살펴 보겠습니다. 나는 "\ n"을 없앴으로써 도움이되지 않았다. 나 또한 .toString()을 시도했는데 그 중 일부와 일부는 사용하지 않았으며 아무 것도 사용하지 않았습니다. – Adam

+0

@ Adam : D "\ n"은 문제가 아닙니다. 문제는 문자열을 객체와 연결하려고한다는 것입니다. 위에서 쓴 것처럼 목록을 반복하고 목록에서 문자열을 선택하여 목록이 아닌 문자열을 연결해야합니다. – desperateCoder

+0

감사! 나는 그것을 더 열심히 보려고 노력할 것이다. 나는 거의 모든 것이 하나의 대상이라고 생각했고, 내가 돌아 오려고 노력한 것에 대해 충분한주의를 기울이지 않았다. 그래도 목록을 반복하려고했습니다. 적어도 나는 Arrays.asList (list)와 함께 내가 생각했다. for 루프가 뒤 따른다. 나는 그렇게 잘못하고있는 것처럼 느꼈다. 어쨌든, 다시 감사드립니다.} – Adam

0
return finalList + punctuation + "\n"; 

가 여기에 문자열 인 것처럼 당신은 List을 치료하는 당신은 유사한 .toString() 또는 뭔가를해야합니까 :

여기에 코드입니까?

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

현재 Arrays.asList