Input Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • javaBookWorm
    New Member
    • Nov 2007
    • 10

    Input Validation

    [CODE=java]import java.applet.App let;
    import java.awt.*; // Contains all of the classes for creating user interfaces and for painting graphics and images.
    import java.awt.event. *;//The root event class for all AWT events.
    import java.lang.Objec t;
    import java.util.Event Object;
    import javax.swing.*;//Provides a set of "lightweigh t" (all-Java language) components that, to the maximum degree possible, work the same on all platforms.


    public class AirlineApplet extends JApplet implements ActionListener {
    JButton printPass;
    JTextArea outputArea;
    String output;
    int seatType, seatNum, choice;
    boolean[] seatArray = {false, false, false, false, false, false, false, false, false, false};

    public void init() {

    Container container = getContentPane( );
    container.setLa yout(new FlowLayout());

    chooseType();

    printPass = new JButton ("Click to print boarding pass");
    printPass.addAc tionListener(th is);

    outputArea = new JTextArea(40, 40);

    container.add(p rintPass);
    container.add(o utputArea);

    }


    public void chooseType() {
    seatType = Integer.parseIn t(JOptionPane.s howInputDialog( "Please type 1 first smoking area or 2 for non-smoking area"));
    selectSeat(seat Type);
    }


    //
    public void actionPerformed (ActionEvent event) {
    outputArea.appe nd(output);

    chooseType();
    }

    public void selectSeat(int z) {

    //argument selects between the smoking and the non-smoking seat

    if (z== 1)
    {
    if (seatArray[0] && seatArray[1] && seatArray[2] && seatArray[3] && seatArray[4])
    {
    choice = Integer.parseIn t(JOptionPane.s howInputDialog( "All smoking section seat has been booked. Would you like non-smoking section? 1 for Yes, 2 for No."));
    if (choice == 1)
    selectSeat(2);
    else
    JOptionPane.sho wMessageDialog( null, "Thank you,Please Come Again.");
    }
    else
    {
    //do smoking seat and make sure that the seat is set to seat unoccupied
    seatNum = (int)(Math.rand om()*5);

    if (!seatArray[seatNum])
    seatArray[seatNum] = true;
    else
    selectSeat(z);

    output = "You have seat " + seatNum + " smoking area. \n";
    }

    }
    else
    {
    if (z== 2)
    if (seatArray[5] && seatArray[6] && seatArray[7] && seatArray[8] && seatArray[9])
    JOptionPane.sho wMessageDialog( null, "There are no seats available in non-smoking area. Sorry.");
    else
    {
    //do non smoking seat and make sure that the seat is seat to unoccupied
    seatNum = (5 + (int)(Math.rand om()*5));

    if (!seatArray[seatNum])
    seatArray[seatNum] = true;
    else
    selectSeat(z);




    output = "You have seat " + seatNum + " in non-smoking area. \n";
    }
    }

    }
    }[/CODE]



    Looking at the following code, i want to do a validation where the user cannot key in Character but only number are allowed, how and where should i write the statements, can you show me the code. I have no idea how to write it in applet. What i mean is that when u run this program, the first message box will come out and ask press 1 or 2 for the seats. My question is when the user type other thing than a number, the program will crashed. So how am i able to solve it?by doing that validation.
    Last edited by Ganon11; Nov 10 '07, 05:34 PM. Reason: Please use the [CODE] tags provided.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at the JFormattedTextF ield class.

    kind regards,

    Jos

    Comment

    • javaBookWorm
      New Member
      • Nov 2007
      • 10

      #3
      Originally posted by JosAH
      Have a look at the JFormattedTextF ield class.

      kind regards,

      Jos
      Thank for the Editing, anyway i am blur on how to write the validation that only number can be accepted to book the seats. If user key in Alphabert the message will say string not allowed. Where and how i should write. Would be happy if you can show me the code.

      Joseph

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by javaBookWorm
        Thank for the Editing, anyway i am blur on how to write the validation that only number can be accepted to book the seats. If user key in Alphabert the message will say string not allowed. Where and how i should write. Would be happy if you can show me the code.

        Joseph
        Did you have a look at the JFormattedTextF ield class then?

        Comment

        Working...