?
Solved

Counting number of characters entered by user at console window

Posted on 2005-03-28
4
Medium Priority
?
238 Views
Last Modified: 2010-03-31
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
Comment
Question by:ViceroyFizzlebottom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13648872
protected members are only accessible from subclasses.
0
 
LVL 6

Author Comment

by:ViceroyFizzlebottom
ID: 13648902
Any suggestion on a specific class or approach to counting the user input?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 13648909
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
 
LVL 6

Author Comment

by:ViceroyFizzlebottom
ID: 13648973
Excellent. Thanks
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question