2011-12-07 4 views
2

아래 코드는 제 코드입니다. 기본적으로 문자가 오름차순인지 내림차순인지 여부를 결정합니다. 소문자 단어가 입력되면 완벽하게 작동하지만, 예를 들어 aB가 입력되면 문자가 분명히있을 때 문자가 orde에 있지 않다는 것을 말합니다. 정말로 확실하지 않고 나는 이것에서 dispair하기 시작하고있다!대문자와 소문자가 혼합 된 문자열 변환 java

text.toLowerCase(); 

    while (! text.equals("END")) 

    {  

     String string = (text); 
     char[] content = string.toCharArray(); 
     java.util.Arrays.sort(content); 
     String sorted = new String(content);   


      if (text.equals(sorted)) 
      { 
       System.out.print(text); 
       System.out.print("  letters in ascending order"); 
       System.out.println(); 

      } 

      else   
      { 
       System.out.print((text)); 
       System.out.print(" letters not in ascending order"); 
       System.out.println(); 
      } 

      System.out.println(); 
      System.out.print("#Enter input text : "); 
      text = BIO.getString(); 
     } 
    } 
} 

답변

3

당신은 다시 text에 값을 저장 (그리고 변환 된 이후 소문자 "end" 확인)해야합니다.

text = text.toLowerCase(); 
while (!text.equals("end")) { // ... 

toLowerCase

오히려 낮은 맡았다 버전을 반환, 원래의 문자열을 수정하지 않습니다. 당신은 유지하려는 경우

또는 "END"는 종료합니다 :

lowered = text.toLowerCase(); 
while (!text.equals("END")) { 
    // ... etc ... 
    text = BIO.getString(); 
    lowered = text.toLowerCase(); 
} 
0

text.compareToIgnoreCase(sorted) == 0를 사용할 수 있습니까?

관련 문제