2014-05-21 1 views
-3

문자열을 가져 와서 대문자, 소문자, 숫자, 마침표, 쉼표, 공백 및 기타 기호를 출력 할 수 있기를 원합니다. 대부분의 코드는 끝났지 만 문제가있는 부분에 대해서는 주석을 달았습니다. 나는 이것을 할 수 없다. 그리고 그것은 주간의 더 좋은 부분을 위해 나를 난처하게했다! 어떤 도움을 주셔서 감사합니다! charString 배열로 변환하여문자열을 가져와 출력하는 방법 : 공백, 대문자, 소문자 등

import java.util.*; 

public class JavaPractice 
{ 

    public static void main(String[] args) 
    { 

     //declarations 

     Scanner keyboard = new Scanner(System.in); 
     char tryAgain = 'n'; 
     char linechar = ' '; 
     String lineText; 
     int uppercase = 0; 
     int lowercase = 0; 
     int digits = 0; 
     int periods = 0; 
     int commas = 0; 
     int blanks = 0; 
     int others = 0; 


     do 
     { 
      // initialize categories  
      uppercase = 0; 
      lowercase = 0; 
      digits = 0; 
      periods = 0; 
      commas = 0; 
      blanks = 0; 
      others = 0; 

      // user input 
      System.out.println("Enter a line of text:"); 
      lineText = keyboard.nextLine(); 

      // This is where I would like to count the number of spaces, uppercase ETC where I am having the most trouble 


      // print output  

      System.out.println("Uppercase:") + (uppercase); 

      System.out.println("Lowercase:") + (lowercase); 

      System.out.println("Digits:") + (digits) 

      System.out.println("Periods:") + (periods); 

      System.out.println("Commas:") + (commas); 

      System.out.println("Blanks:") + (blanks); 

      System.out.println("Other Symbols":) + (others); 

      // try again   

      System.out.println("Would you like to try again y/n?"); 

      tryAgain = keyboard.nextLine().charAt(0); 

     } while (tryAgain == 'y' || tryAgain == 'Y'); //end while loop 

     System.out.println("GoodBye"); 

    } //end main() 
} //end JavaPractice 
+1

* 어떻게 * 문제가 있는지 말할 수 있습니다. – awksp

+1

더 구체적으로 말하자면, 실제로 무언가를 코딩하려고 시도하고 빈 영역으로 오지 않으려 고 할 수 있습니다 ... – awksp

답변

0

먼저 Chars 배열로 문자열을 만드십시오.

대문자 및 소문자의 경우 문자가 카운터를 증가시킬 때 A - Z에서 루프를 만들 수 있습니다.

마침표는 쉼표와 공백으로 동일하게 적용 할 수 있습니다. 즉, String의 길이를 반복하고 마침표, 쉼표 및 비어있는 항목을 찾습니다. 더 많은 사전 솔루션을 원하시면 RegEx를 살펴보십시오.

-1

String 클래스의 split 함수를 사용하여 특정 정규식 입력에 따라 문자열을 분할하여 배열을 반환합니다.

는 당신이 유사한 다른 문자에 따라 분할 할 수 있습니다 String

String input="hello.world"; 

// use different delimeters 
// splitting the input based on a comma 
String[] test = input.split(","); 

// test will now have the value test[0] = hello , test[1]= world 

int length = test.length - 1 // test.length = 2, you need to subtract 2 because the length of the array will always be 1 more than the number of occurences, so if the number of occurances is n the length of the array is n+1. 

다음과 같은 예를 들어 고려하십시오.

+0

"a, b, c, d, e,. .. ". (단, 구분 기호는 일치하지 않으므로'test'는'hello.world "라는 문자열 하나만 포함 할 것입니다.) 어떤 시점에서는 형식을 결정하기 위해 여전히 문자가 필요합니다. – cHao

+1

그것은 구두쇠 함수가 문자열 함수를 이해하기위한 해킹 된 해결책이었습니다. 구분 기호 매개 변수에 기초한 문자열 분할. 입력 문자열을 유지하고 복사 문자열에 대한 작업을 수행하면 모든 작업을 수행 할 수 있습니다. 나는 그것이 가장 효율적인 메모리 솔루션이 아니라는 것에 동의한다. – user1801279

+0

메모리 효율은 여기서 문제가되지 않는다. 문제는 다음과 같습니다. * 좋아, 문자열을 분할했습니다. 축하해. 이제 무엇을 할 수 있겠습니까? * 분할은 목표에 더 가까이 다가 가지 못했습니다. 셀 수는 하위 문자열이 많습니다. 그리고 그 부분 문자열을 위해 작동 할 것이고, 또한 분리되지 않은 문자열을 위해 작동해야합니다. 만약 당신이 문자 (예를 들어, 단어)보다 더 큰 것을 세지 않으려 고한다면, 정말로 나누는 것은 문제를 복잡하게 만듭니다. – cHao

관련 문제