0

So I have this exercise:

"Create a program that reads characters from the keyboard until it receives a dot. The program must count the number of spaces. Indicate the total number of spaces at the end of the program."

My problem is, everytime I type a character, I have to press "Enter", for it to register. This way it acknoledges the dot, but not the spaces, and it's also not practical, the point is to type the whole sentence, and ackowlegde the dot, and proceed with the rest of the code.

If I type the whole sentence (ex.: "I ate food."), it does not acknowledge the dot, and lets me keep writing.

This link: Java Scanner: stop reading after the first entry, suggests reading character by character, which is a thing I don't want.

This link: Java Scanner stop reading input if receives bad character, this one did not help me either.

package m1_praticas;

import  java.util.Scanner;

public class M1_Pratica3_ContarEspacos {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char input;
        String sentence = "";
        //int white_spaces = 0;

        System.out.println("Escreva uma frase:");
        do{
            input = scanner.next().charAt(0);
            sentence += input;
        } while(input != '.');

        //for(char c : sentence.toCharArray()){
            //if(c == ' '){
                //white_spaces++;
            //}
        //}
        System.out.println(sentence);
        System.out.println(white_spaces);
    }
}

In short, what I want, is when the user is typing a sentence, there is someking of method that is reading the sentence charater, by character. Once that method meets the dot, it performs the rest of the code. Is that possible, or do I always have to hit "enter", to register a key?

Thanks in advance.

  • Why not read text line by line (you will still need to press Enter but only at the end of each line)? You can read lines in outer loop and process each character in line via another loop. When you find `dot` stop outer loop (you can do it via `return` or `break` - this may be helpful [How do I break out of nested loops in Java?](https://stackoverflow.com/q/886955)) – Pshemo May 25 '22 at 17:29
  • @Pshemo, thanks, but won't do, it needs to be as the exercise states, bc it's to be reviewed by a professor. If it was a personal project, I would consider it, yes. – OrlandoVSilva May 25 '22 at 17:55
  • Fact that it will be reviewed doesn't mean it is wrong. Your assignment doesn't force you to *read* one character at a time, but to *process* one character at a time (so you can drop rest or characters after first dot). – Pshemo May 25 '22 at 17:59
  • Other option os to write some GUI (for instance with `swing` package) and create listeners for keys (console doesn't have that option so we are forced to confirm input with Enter). – Pshemo May 25 '22 at 18:09
  • *Once that method meets the dot, it performs the rest of the code. Is that possible* **No** *or do I always have to hit "enter", to register a key?* **Yes** – g00se May 25 '22 at 18:27

0 Answers0