0

Can someone help me please. I am using a Scanner Class and want to count characters entered in the textfield. Unfortunately, backspace and any key (Ctr,Alt,Capslock) for that matter is counted as a character and that increments the counter by 1.

Here is the code

String ab="^[A-Za-z0-9]+$";

    public void keyReleased(KeyEvent ke) throws NoSuchElementException{
                           Scanner scann=new Scanner(Jtextfield.getText());
                                  
   {
   String sarwa=scann.next(ab);

   if(true)


    i++;
    champo.setText("  "+Integer.toString(i));

   }

Thankyou,
ARZ

Edited by Ezzaral: Fixed code tags.

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by ARaza110
0

Do you have a small test program with a text field that compiles and executes to demonstrate the problem?

0

Since the previous character (the one erased) and the backspace are both counted, then the input stream must be in "raw" as opposed to "cooked" mode. Basically, you need to change the input attributes so that the backspace really removes the previous character from the string/stream. If it doesn't, then you will need to create a callback to handle that for you.

0

Can you please elaborate a little, How should i change the input attributes?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.