Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create an applet which takes number from text field and validates it

  1. /*-*-*-*-*-*-*-*-*--*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  2. Create an applet which takes number from text field and on clicking button labelled
  3. "read" checks whether the number is in range of 0 to 100 or not. If not it should
  4. throw customized exception "Out of Range...". If exception is thrown, put message
  5. "Enter Number within range 0 to 100" in text field.
  6. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
  7.  
  8. import java.applet.Applet;
  9. import java.awt.Button;
  10. import java.awt.TextField;
  11. import java.awt.event.ActionEvent;
  12. import java.awt.event.ActionListener;
  13.  
  14. /*  <applet code = "CheckNumber" width= 400 height = 400>
  15.  </applet>
  16. */
  17.  
  18. public class CheckNumber extends Applet implements ActionListener
  19. {
  20.  TextField t1;
  21.  Button b1;
  22.  public void init()
  23.  {
  24.   t1 = new TextField(50);
  25.   add(t1);
  26.   b1 = new Button("Read");
  27.   add(b1);
  28.   b1.addActionListener(this);
  29.  }
  30.  public void actionPerformed(ActionEvent e)
  31.  {
  32.   if(e.getSource() == b1)
  33.   {
  34.    try
  35.    {
  36.     check(Integer.parseInt(t1.getText()));
  37.     t1.setText(t1.getText() + " OK...");
  38.    }
  39.    catch(NoOutOfRange ex)
  40.    {
  41.     t1.setText("Enter number bewteen 0 to 100...");
  42.    }
  43.    catch(Exception ex)
  44.    {
  45.     t1.setText("Invalid Number");
  46.    }
  47.   }
  48.  }
  49.  static void check(int val) throws NoOutOfRange
  50.  {
  51.   if(val < 0 || val > 100)
  52.   {
  53.    throw new NoOutOfRange("Range");
  54.   }
  55.  }
  56. }
  57.  
  58. class NoOutOfRange extends Exception
  59. {
  60.  String msg;
  61.  NoOutOfRange(String s)
  62.  {
  63.   msg = s;
  64.  }
  65.  public String toString()
  66.  {
  67.   return("No. Out Of Range : " + msg );
  68.  }
  69. }
Your rating: None Average: 1 (7 votes)
Share this

Post new comment

2 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.