2016-11-15 849 views
2

사용자 입력에서 여러 줄의 텍스트를 읽고 알파벳의 각 글자가 문자열에 표시된 횟수를 표시하는 프로그램을 만들고 있습니다. 마지막 줄은 마침표 값으로 사용되는 마침표로 끝납니다. 이것은 내가 지금까지 가지고있는 것입니다 :사용자 입력에서 여러 줄의 텍스트를 가져 오는 방법이 있습니까?

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 


public class LetterCounter 
{ 

    public static void main(String[] args) 
    { 

    try { 
      BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); 

      String lineOfText; 
      char letter, choice; 

      int countArray[] = new int[26]; 
      do 
      { 

       System.out.println("Enter the String (Ends with .):"); 
       while ((lineOfText = input.readLine()) != null) 
       { 

        for (int i = 0; i < lineOfText.length(); i++) 
        { 
         letter = lineOfText.charAt(i); 
         int index = getIndex(letter); 
         if (index != -1) 
         { 
          countArray[index]++; 
         } 
        }   
        if (lineOfText.contains(".")) 
         break; 
       } 

       System.out.println("Count Alphabets:"); 

       for (int i = 0; i < countArray.length; i++) 
       { 
        System.out.println((char) (65 + i) + " : " + countArray[i]); 
       } 

       //Ask user whether to continue or not 
       System.out.println("\nWould you like to repeat this task? (Press y to continue or n to exit program): "); 

      choice = input.readLine().toLowerCase().charAt(0); 

       System.out.println(); 

      } while (choice == 'y'); 

     } 

     catch (IOException e) 
     { 
      // Auto-generated catch block 
      e.printStackTrace(); 
     }  
    } 


    //method to get the index of alphabet 

    public static int getIndex(char letter) 
    { 
     if (Character.isAlphabetic(letter)) 
     { 
      if (Character.isUpperCase(letter)) 
      { 
       return (int) letter - 65; 
      } 

      else 
      { 
       return (int) letter - 97; 
      } 
     } 

     else 
     { 
      return -1; 
     } 

    } 
} 

나는 여러 줄의 텍스트를 허용하는 것이 궁금합니다. 그래서 한 줄을 할 수 있습니다.

+0

http://stackoverflow.com/questions/14169661/read-complete-file-without-using-loop-in-java –

+0

텍스트 파일을 사용하지 않기 때문에 전체 파일을 읽을 수 있습니다. – Derek

+0

'스캐너'가 도움이 될 수도 있습니다. http://stackoverflow.com/questions/2296685/how-to-read-input-with-multiple-lines-in-java – Philipp

답변

-1

1 행을 읽은 후 버퍼에 데이터가 없기 때문에 BufferedReader는 null을 반환합니다.

"java.util.Scanner"클래스는 행이 사용 가능할 때까지 스레드를 잠그고 프로세스에서 null 데이터를 반환하지 않는 경우에 더 적합합니다.

0

첫 번째 입력에서 a에 도달 할 때 프로그램이 중지되도록 스캐너의 구분 기호를 변경할 수 있습니다. 그는

Scanner sc = new Scanner(System.in); 
    char choice = 'y'; 

    while (choice == 'y'){ 
     System.out.println("Enter the String (Ends with .):"); 
     sc.useDelimiter("\\."); 
     System.out.println(sc.next()); 
     //Ask user whether to continue or not 
     System.out.println("\nWould you like to repeat this task? (Press y to continue or n to exit program): "); 
     sc.useDelimiter("\n"); 
     sc.next(); 
     choice = sc.next().toLowerCase().charAt(0); 

    } 
+0

내 문제는 새 줄을 시작하기 위해 "Enter"키를 누르면 ''로 끝낼 수 없다는 것입니다. 텍스트 줄을 끝내십시오. 그래서 여러 줄을 나타 내기 위해 한 줄에 텍스트를 씁니다. – Derek

+0

잘 모르겠습니다. 이 구현은'.'이있을 때까지 여러 행을 읽습니다. 그렇지 않으면'.' 다음에'\ n '이있을 때 읽기를 멈추고 구분 기호를'\. \\ n'으로 변경합니다. 이것은'.' 또는'\ n'이있는 경우 계속해서 읽지 만'. \ n'이 있으면 멈 춥니 다. –

0

이 프로그램은 지정된대로 작동하는 것 같다 계속하고 싶은 경우도 때 라인 읽기를 정지 제외하고 다음 확인을 다시 기본값으로 이동합니다. 선의 중간에있다.