有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

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) 个答案

  1. # 1 楼答案

    您可以更改扫描仪的定界符,以便在第一次输入时,程序在到达指定位置时停止。然后返回默认设置,检查他是否愿意继续

        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);
    
        }
    
  2. # 2 楼答案

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

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

    该程序似乎按照指定的方式工作,但当出现错误时,它也会停止读取行。在一条直线的中间。

  4. # 4 楼答案

    读取1行后,BufferedReader将返回null,因为其缓冲区中没有数据可以转换为行

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