I have made a keyTyped event java program. Now it display square for each single number i typed through keyborad.
What i want is this:

  1. when i type 1 answer to be displayed 1
  2. Again when i type 2 answer to be 144, not 4.

So any Idea how to get it right (I don't know how to combine the previously entered key with new ones)

private void simpleNumberKeyTyped(java.awt.event.KeyEvent evt) {                                      

            int n=0;

            char k=evt.getKeyChar();

    if (!(k>='0' && k<='9'))
            evt.consume();

    else
    {
                    if(k=='0')
                        n=0;
                    if(k=='1')
                        n=1;
                    if(k=='2')
                        n=2;
                    if(k=='3')
                        n=3;
                   if(k=='4')
                        n=4;
                   if(k=='5')
                        n=5;
                   if(k=='6')
                        n=6;
                   if(k=='7')
                        n=7;
                   if(k=='8')
                        n=8;
                   if(k=='9')
                        n=9;

}

        int ans = n * n;
        square.setText(String.valueOf(ans));


    }  

          `

Recommended Answers

All 8 Replies

** re-calculated with each keystroke**

keep an int on class scope, that way the value will remain through running your method several times:

just to give you an idea:

int aid = 0;
private void simpleNumberKeyTyped(java.awt.event.KeyEvent evt) {
  if ( aid != 0 )
    aid *= 10;
 int n=0;           
 char k=evt.getKeyChar();   
 if (!Character.isDigit(k) )        // a better check    
 evt.consume();    
 else    {                    
 if(k=='0')    aid+==0;                   
 else if(k=='1')   aid +=1; // added the else, if it already is known to be 0, you don't need the next if's to be
 //executed, it improves the efficiency

}
int ans = aid*aid;
// you may want to choose another (larger) data type
square.setText(String.valueOf(ans));
}

A small hint: conversion of char to the int value it represents:
We know that char is a numeric type, and we also know that '0' to '9' are consecutive in UniCode, so once you have checked that k is a digit you can safely replace all thise if/elses with
n = k - '0';

thanks stultuske for suggesting to

keep an int on class scope
It solved my problem. But I am sorry to say that your code isn't giving me the square, instead some 0s.

May be I didn't understand your code esp. line 10

if(k=='0') aid+==0;

giving me error. Or is it supposed to be aid+=0;

Anyways I used your idea in my original code and it is working.
Only giving me weird answer when backspace key is pressed. Working on it.

Thanks for your help.

Any Idea how to detect if backspace key is pressed for my code?

a KeyListener that checks the value of the char entered.

a KeyListener that checks the value of the char entered.

Can you please explain it through some coding?

check this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.