• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

Counting number of characters entered by user at console window

Thanks in advance for any help.
I'm trying to get the number of characters a user has typed. I tried the following:
import java.util.*;
import java.io.*;

public class BufferSize
{
  public static void main(String[] args)
  {
    int numChars = 0;
    BufferedInputStream in = new BufferedInputStream(System.in);
     try
     {
        in.read();
     }
     catch (IOException e)
     {
     }
     finally
     {
       numChars = in.count;
     }
      System.out.printf("Number of characters: \n", numChars);
    }
}

The compiler gripes that count is a protected member of BufferedInuputStream class. Now, I thought that protected meant that if I created
an instance of the class, then my instance would have access to the variable. Some clarification on this would be helpful as well as
any advice on getting that character count with or without using the BufferedInputStream.
0
ViceroyFizzlebottom
Asked:
ViceroyFizzlebottom
  • 2
  • 2
1 Solution
 
objectsCommented:
protected members are only accessible from subclasses.
0
 
ViceroyFizzlebottomAuthor Commented:
Any suggestion on a specific class or approach to counting the user input?
0
 
objectsCommented:
System.in is buffered so your code won't get notified of input until enter is pressed.
So you could use a BuffereredReader and it's readLine() method to read the entered line and take the length of the string.
0
 
ViceroyFizzlebottomAuthor Commented:
Excellent. Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now