2

I'm creating a program that needs to read multiple lines of text from user input and display the number of times each letter from the alphabet is seen in the string. The last line ends with a period, which will serve as a sentinel value. This is what I have so far:

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

   }
}

I was wondering how I would allow multiple lines of text. So for I can do one line.

Derek
  • 45
  • 1
  • 5

4 Answers4

0

You can change the delimiter of the Scanner so that on the first input, the program stop on reaching a . then go back to the default to check if he would like to continue

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

    }
Aimee Borda
  • 842
  • 2
  • 11
  • 22
  • My problem is that if I press "Enter" to start a new line, I wouldn't be able to end with a '.' to end the line of text. So would I just write the text in just one line to represent multiple lines? – Derek Nov 15 '16 at 23:36
  • Not sure I understand the problem. This implementation reads multiple lines until there is a `.` otherwise if you want to stop reading when there is a `.` followed by an `\n` change the delimiter to `'\.\\n`. This continues to read if there is either an `.` or a `\n` but stops if there is a `.\n` – Aimee Borda Nov 16 '16 at 06:03
0

The program seems to work as specified, except it also stops reading lines when a . is in the middle of a line.

Wiebe
  • 106
  • 3
0

You can use hasNext() concept of Java here.

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

while(scan.hasNext()){
    check = scan.nextLine();
    System.out.println(check);
Shashank
  • 1
  • 3
-1

After 1 line has been read, the BufferedReader will return null since there is no data in its buffer that it can turn into a line.

The "java.util.Scanner" class is better suited, where it will lock the thread until a line is available, and not return null data in the process.

This_Is_Alex_
  • 305
  • 1
  • 6