사용자 입력에서 여러 줄의 텍스트를 읽고 알파벳의 각 글자가 문자열에 표시된 횟수를 표시하는 프로그램을 만들고 있습니다. 마지막 줄은 마침표 값으로 사용되는 마침표로 끝납니다. 이것은 내가 지금까지 가지고있는 것입니다 :사용자 입력에서 여러 줄의 텍스트를 가져 오는 방법이 있습니까?
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;
}
}
}
나는 여러 줄의 텍스트를 허용하는 것이 궁금합니다. 그래서 한 줄을 할 수 있습니다.
http://stackoverflow.com/questions/14169661/read-complete-file-without-using-loop-in-java –
텍스트 파일을 사용하지 않기 때문에 전체 파일을 읽을 수 있습니다. – Derek
'스캐너'가 도움이 될 수도 있습니다. http://stackoverflow.com/questions/2296685/how-to-read-input-with-multiple-lines-in-java – Philipp