2014-09-07 4 views
-1

배열의 모든 단어에서 모든 모음을 삭제하고 싶습니다. 그래서이 함께했다 : 나는, 나는이 하나가 최고 였는데, 아직도 내가이 일을 할 수없는 시도 모든 옵션에서루프 내부의 루프/바꾸기가 작동하지 않습니다

public class AnotherExercise { 

public static void main (String[] args){ 

    String[] intruments = {"cello", "guitar", "violin", "double bass"}; //the array of words 
    String[] vowels = {"a", "e", "i", "o", "u"}; // the array of vowels 
    String[] intruments2 = new String[5]; //the array of wrods without vowels 
    String nothing = ""; // used to replace a vowel to nothing 

    for (int i = 0; i < 4; i++){ // first for loop to go through all of the intruments 

     for(int e = 0; e < 5; e++){ //second for loop to go through all the vowels 

     intruments2[i] = intruments[i].replaceAll(vowels[e], nothing); //replacing vowels 
     //and storing them in a new array 
     } 

     System.out.println(intruments2[i]); //outputting the words without vowels 
    } 

} 

} 

, 그것은 출력 :

cello 
    gitar 
    violin 
    doble bass 

을 내 의견에 이상한 부분은 "u"를 대체한다는 것입니다. 어리석은 실수 일지 모르지만 나는 그것을 이해할 수 없다.

+0

이것은 대단히 비효율적입니다. 단어마다 모음 하나당 하나의 검색을 수행하는 대신 수동으로 문자를 반복하여 모음에 대해 테스트 해보십시오. 또는 하나의 regex replace를 사용하여 모든 모음을 지우십시오. –

답변

0

메소드 문자열. ReplaceAll은 첫 번째 인수로 정규식을받습니다. 버전의 문제에 대해, 그래,

public class AnotherExercise { 

    public static void main (String[] args){ 

     String[] intruments = {"cello", "guitar", "violin", "double bass"}; 
     String[] intruments2 = new String[5]; 
     String nothing = ""; 

     for (int i = 0; i < 4; i++){ 

      intruments2[i] = intruments[i].replaceAll("a|e|i|o|u", nothing); 

      System.out.println(intruments2[i]); 
     } 

    } 

} 

을 그리고 : 그것은 당신이 한 번에 모든 모음을 제거 할 수 있습니다. 내부 루프에서 항상 문자열의 대체 버전을 적용합니다. 그래서 당신은 마지막 시도에서 결과를 얻습니다 - 편지 'u'.