Input streams Count words

Read an input text from the console and print the number of words. By word we mean a sequence of characters separated by one or several spaces.

If the input is empty or there are no characters except spaces, print 0.

Sample Input 1:

one two three

Sample Output 1:

3

Sample Input 2:

between   us  several   space characters

Sample Output 2:

5
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;

class Main {
    public static void main(String[] args) throws Exception {
        try (Reader reader = new BufferedReader(new InputStreamReader(System.in))) {
            
            // Reader is supertype of BufferedReader 
            // and BufferedReader is the actual object created 
            // at runtime so we are free to cast reader to BufferedReader
            // to get the readLine() functionality
            String line = ((BufferedReader) reader).readLine();

            // read line is only white spaces or empty (length == 0) return 0
            // otherwise we remove all leading and trailing white spaces
            // from line to use split by white space (1 or more)
            int result = line.isBlank() ? 0 : line.trim().split("\\s+").length;
            
            System.out.println(result);
        }
    }

}

hi i've been reading on superclass and subclass reference and there's this thing i still don't quite get can i ask, why do we use superclass reference (Reader reader) if we already know before runtime that the object would be its subclass BufferedReader?

You will hear often programmers say "Program to an Interface (abstract class)" and I asked my self the same question "Why should I do that?".

You and I, we know what exactly we want to use, here we are using BufferedReader which is one of those classes that extends Reader abstract class.

But what if someone else came a half a year later and thought to himself hem... I don't want to use BufferedReader I want to use something else say CharArrayReader (this one also extends Reader) then he sees this BufferedReader bf = new BufferedReader(...)

and all the functionality we wrote there is a chance that they are only for BufferedReader so the other programmer can't just come and change it to CharArrayReader without starting to change the functionality of the method. He is frustrated because our method is not flexible and its "locked" and works only with BufferedReader. He slams the keyboard and walks away.

So, if we programed to an interface Reader everyone could come and choose their preferred implementation class, Reader reader = CharArrayReader, BufferedArray, InputStreamReader etc. without changing a single line of our method because all methods we used are of Reader and each class implementation implementing the same method a little different.

When I thinking about this is like: Interface or Abstract class is a Socket and what you choose goes into that socket. 😄 Interface -( O implementation

posted @ 2020-08-23 18:13  longlong6296  阅读(136)  评论(0)    收藏  举报