2013-03-07 4 views
0

각 문자의 발생 횟수를 계산하는 방법에 대해 실제로 확신 할 수는 없습니다. 누군가가이 일에 올바른 방향으로 날 지점 수, 문자열을 통해 :)문자열의 각 문자 수를 어떻게 계산합니까?

if(icanreadstrings.equals ("start")){ 

      String strten = Console.readLine("Enter 10 letters (no spaces,commas between each letter and no numbers): "); // Asks the user to enter 10 letters which is then saved as a string "strten". 
      Console.println(strten);// Prints out whatever the user entered. 

      if(strten.matches("[a-zA-Z]+") && strten.length() == 10){ // if whatever the user enters is letters (not numbers) and if it matches to the length, which is 10. 
       Console.println("There are " + strten.length() + " letters that you've wrote."); // lets the user continue if they've wrote 10 letters, yes 10! 

      }else{ 
       Console.println("There are more than or less than 10 letters that you've wrote or you have numbers in your list of letters. Please rewrite 10 different LETTERS for this program to continue."); // Checks to see if you wrote more than 10 letters if you did then it prompts a message and the user can't continue until he writes 10 letters. 
       } 


     } 
+2

* "누군가 올바른 방향으로 가리킬 수 있습니까?"* 1) ['String'] (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html) 2) [언어 기본 사항] (http://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html) -보다 구체적인 도움을 얻으려면보다 구체적인 질문을하십시오. –

+1

http://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string?rq=1 – Danny

+1

여러 번 물었습니다. -이 페이지의 오른쪽 상단에 검색 창이 있습니다. 예를 들면 다음과 같습니다 :'[java] count occurence character string' – assylias

답변

1

사용하십시오 Map<Character, Integer>, 루프 감사합니다. 문자가 처음 나타날 때 문자와 1을지도에 넣습니다. 후속 발생시 맵에 이미있는 문자의 값에 1을 더하십시오.

지도의 입력 세트가 나중의 데이터를 제공합니다.

관련 문제