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:
- when i type 1 answer to be displayed 1
- 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));
}
`