有没有办法从用户输入获取多行文本?

时间:2016-11-15 18:58:01

标签: java arrays string bufferedreader

我正在创建一个程序,需要从用户输入中读取多行文本,并显示字符串中每个字母的字母数。最后一行以句点结束,该句号将作为标记值。这就是我到目前为止所做的:

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;
       }

   }
}

我想知道如何允许多行文字。所以我可以做一行。

4 个答案:

答案 0 :(得分:0)

您可以更改扫描仪的分隔符,以便在第一个输入时,程序在到达时停止。然后返回默认值以检查他是否愿意继续

    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);

    }

答案 1 :(得分:0)

该程序似乎按指定的方式工作,但它在a时也会停止读取行。在一条线的中间。

答案 2 :(得分:0)

您可以在此处使用hasNext() Java概念。

Scanner scan = new Scanner(System.in);
String check = "\0";

while(scan.hasNext()){
    check = scan.nextLine();
    System.out.println(check);

答案 3 :(得分:-1)

读取1行后,BufferedReader将返回null,因为缓冲区中没有数据可以变成一行。

&#34; java.util.Scanner&#34; class更适合,它会锁定线程,直到一行可用,并且不会在进程中返回null数据。