【问题标题】:Is there any way to get multiple lines of text from user input?有没有办法从用户输入中获取多行文本?
【发布时间】:2017-03-29 19:04:45
【问题描述】:

我正在创建一个程序,该程序需要从用户输入中读取多行文本并显示字母表中每个字母在字符串中出现的次数。最后一行以句点结尾,将用作标记值。这是我目前所拥有的:

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

   }
}

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

【问题讨论】:

标签: java arrays string bufferedreader


【解决方案1】:

您可以更改 Scanner 的分隔符,以便在第一次输入时,程序在到达 .然后回到默认,看看他是否愿意继续

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

    }

【讨论】:

  • 我的问题是,如果我按“Enter”开始新行,我将无法以 '.' 结尾。结束文本行。那么我是否只将文本写在一行中以表示多行?
  • 不确定我是否理解问题所在。此实现读取多行,直到出现.,否则如果您想在出现. 后跟\n 时停止读取,请将分隔符更改为'\.\\n。如果有.\n,则继续读取,但如果有.\n,则停止
【解决方案2】:

该程序似乎按照指定的方式工作,除了它还会在 .在一行的中间。

【讨论】:

    【解决方案3】:

    您可以在这里使用hasNext() Java 的概念。

    Scanner scan = new Scanner(System.in);
    String check = "\0";
    
    while(scan.hasNext()){
        check = scan.nextLine();
        System.out.println(check);
    

    【讨论】:

      【解决方案4】:

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

      “java.util.Scanner”类更适合,它会锁定线程直到有一行可用,并且在进程中不返回空数据。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        • 1970-01-01
        • 2019-10-18
        • 2014-09-26
        相关资源
        最近更新 更多