2
import java.io.*;

public class Page117
{

    public static void main(String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        char letter;
        String sentence;
        int counter = 0;
        char response = 'o';

        do
        {
            System.out.print("Type a sentence: ");
            sentence = input.readLine();
            System.out.print("Type a character to find: ");
            letter = (char) input.read();

            for(int x=0;x<sentence.length();x++)
            {
                if(letter==sentence.charAt(x))
                {
                    counter++;
                }
            }

            System.out.println("Number of times " + letter + " occured is: " + counter);
            System.out.print("Continue? [y/n]: ");
            response = (char) input.read();

        }while(response != 'n');          
    }        
}

About the program: The user will enter a sentence. The user will also input a character and count how many of that character occurs in the sentence.

I've come across a little problem. How do I make it so that after the process, it allows me to input my response. Because after I enter the character and tells me the number of occurrences, it either exits or won't allow me to input anything.

I've tried almost everything in my code. I don't use do-while loops that much so this is kind of difficult for me. I don't use booleans that much either.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
NoobCoder
  • 73
  • 1
  • 9
  • 1
    Hint: when you answer "Type a character to find:" you probably type some letter and hit [Return], this return is in fact an end-of-line char, see https://ideone.com/ePYWSE Note: you should use a `Scanner` –  Aug 13 '16 at 11:06
  • May best suggestion is to read up on interactive input. You may want to use a Scanner. My suspicion is, if you type, say, t, to have the program count letter t in your sentence, and the a carriage return, then the carriage return will be read as the response in the end and compared to `'n'`. You can check such assumptions using a debugger or some temporary System.out.println() statements. – Ole V.V. Aug 13 '16 at 11:07
  • The number of occurrences are incorrect too, since you don't clear your *counter* variable for each run. – theabhinavdas Aug 13 '16 at 11:07

3 Answers3

2

The problem that you are facing is related to the fact that you read one character while you have several characters that are provided: the input character + the carriage return.

So simply replace this

letter = (char) input.read();

With something like this:

letter = input.readLine().charAt(0);

Indeed calling readLine() will make the reader read the entire line (the carriage return included) and will return only what you have before the end of line corresponding to your input character here.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
2

@Ole V.V is right. The carriage return that the user enters after he inputs the first time is being read at the end of the loop. The below code works. Also counter needs to be initialized within the loop.

public class Page117 {

    public static void main(String[] args) throws IOException
    {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        char letter;
        String sentence;

        do
        {
            int counter = 0;
            char response = 'o';
            System.out.print("Type a sentence: ");
            sentence = input.readLine();
            System.out.print("Type a character to find: ");
            letter = input.readLine().charAt(0);

            for(int x=0;x<sentence.length();x++)
            {
                if(letter==sentence.charAt(x))
                {
                    counter++;
                }
            }

            System.out.println("Number of times " + letter + " occured is: " + counter);
            System.out.print("Continue? [y/n]: ");

        }while(input.readLine().charAt(0) != 'n');

    }
}
Kiran K
  • 703
  • 4
  • 11
1

You could use Scanner instead, the following code waits for the input as intended. Also you probably want to reset the counter for a new sentence:

Scanner input = new Scanner(System.in);

char letter;
String sentence;
int counter = 0;
char response = 'o';

do
{
    counter = 0; // reset the counter for a new sentence

    System.out.print("Type a sentence: ");
    sentence = input.nextLine();
    System.out.print("Type a character to find: ");
    letter = (char) input.nextLine().charAt(0);

    for(int x=0;x<sentence.length();x++)
    {
        if(letter==sentence.charAt(x))
        {
            counter++;
        }
    }
    System.out.println("Number of times " + letter + " occured is: " + counter);
    System.out.print("Continue? [y/n]: ");
    response = (char) input.nextLine().charAt(0);

}while(response != 'n');  

input.close();
Frederic Klein
  • 2,846
  • 3
  • 21
  • 37