The Artima Developer Community
Sponsored Link

Java Answers Forum
catch illegal numerical input??

1 reply on 1 page. Most recent reply: Jan 14, 2004 5:47 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
morki

Posts: 4
Nickname: sheva7
Registered: Jan, 2004

catch illegal numerical input?? Posted: Jan 13, 2004 11:25 AM
Reply to this message Reply
Advertisement
Can anyone give an example with this fragment of code:

try
{
intBet = Integer.parseInt( txtBet.getText() );
}
catch (NumberFormatException e)
{
message = "The bet you entered is not a legal
number!";
}

A user inputs for example a char in a textfield instead of an integer. the code above must catch this

plz help, thx!!


mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: catch illegal numerical input?? Posted: Jan 14, 2004 5:47 AM
Reply to this message Reply
So u will not code it by urself...u have decided???-- :-) simply saying that man
//First implement KeyLisnter in ur class.
//then add keyListner to ur textField as
txtBet.addKeyListener(this);
 
//now implement all the methods of keyListner. in the class.
 
public void keyPressed(KeyEvent e)
{
 
 int i = e.getKeyCode();
  if(i=1 || i=2....)//check
 {
  //Kick the user.
 } 
//or u can do it other way
 
 try
{
intBet = Integer.parseInt( txtBet.getText() );
}
catch (NumberFormatException e)
{
message = "The bet you entered is not a legal
number!";
}
public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}
 
 

Flat View: This topic has 1 reply on 1 page
Topic: Can't get my subwindows to show up Previous Topic   Next Topic Topic: J2EE S/w & H/w Needed

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use