How to write validation where String/char cant be accepted

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

    #1

    How to write validation where String/char cant be accepted

    [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]


    I wanna ask how am i able to do the validation when the message box out asking the user what type of seats they want, when user key in other character than a number it should display a message to user that Char are not accepted only number accepted. Anyone can show me the code, i would appreciate.Than k

    Joseph
    Last edited by Ganon11; Nov 11 '07, 03:47 PM. Reason: Please use the [CODE] tags provided.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by javaBookWorm
    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";
    }
    }

    }
    }


    I wanna ask how am i able to do the validation when the message box out asking the user what type of seats they want, when user key in other character than a number it should display a message to user that Char are not accepted only number accepted. Anyone can show me the code, i would appreciate.Than k

    Joseph
    1.) Use code tags when posting code
    2.) You could parse the String using Integer.parseIn t, if the String is not a number then an Exception will be thrown. Alternatively, you could use a regular expression.

    Comment

    • javaBookWorm
      New Member
      • Nov 2007
      • 10

      #3
      Originally posted by r035198x
      1.) Use code tags when posting code
      2.) You could parse the String using Integer.parseIn t, if the String is not a number then an Exception will be thrown. Alternatively, you could use a regular expression.

      Thank you for your help, But i don't really understand what you mean by using regular expression. Would you show me the code by typing it in red words. Thank you.I really newbie to IOException packages, i don;t really know how to apply. Would be happy if you can teach me.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        I wouldn't worry about regular expressions right now - using Exception handling is a lot easier. Here's a quick tutorial for using expressions.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by javaBookWorm
          I wanna ask how am i able to do the validation when the message box out asking the user what type of seats they want, when user key in other character than a number it should display a message to user that Char are not accepted only number accepted. Anyone can show me the code, i would appreciate.Than k

          Joseph
          Basically when I enter "Fronobulax " your program is not supposed to accept my
          input and when I enter "42", everything is supposed to be fine.

          Have a look at the Integer.parseIn t(String s) method. It throws a particular
          exception, a NumberFormatExc eption when the input doesn't make sense as a
          number. Otherwise it simply returns the number represented by the String it was
          fed.

          A bit of try { ... } catch { ... } logic would do the rest; I'd prefer a little separate class
          for this functionality but that's just me; feel free to ignore that.

          kind regards,

          Jos

          Comment

          • javaBookWorm
            New Member
            • Nov 2007
            • 10

            #6
            Originally posted by JosAH
            Basically when I enter "Fronobulax " your program is not supposed to accept my
            input and when I enter "42", everything is supposed to be fine.

            Have a look at the Integer.parseIn t(String s) method. It throws a particular
            exception, a NumberFormatExc eption when the input doesn't make sense as a
            number. Otherwise it simply returns the number represented by the String it was
            fed.

            A bit of try { ... } catch { ... } logic would do the rest; I'd prefer a little separate class
            for this functionality but that's just me; feel free to ignore that.

            kind regards,

            Jos

            Dear Chief Editors,
            Would you show me some examples, how can i write the code. Because i am self-learning the Java language.i know about IOException but i don't know how to use it.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by javaBookWorm
              Dear Chief Editors,
              Would you show me some examples, how can i write the code. Because i am self-learning the Java language.i know about IOException but i don't know how to use it.
              Suppose you have a String 's' and you want to convert it to an integer; you don't
              know whether the content of 's' makes sense as an int number; you could do this:

              [code=java]
              public int asInteger(Strin g s) {
              try {
              return Integer.parseIn t(s); // hope for the best
              }
              catch (NumberFormatEx ception nfe) {
              // 's' wasn't a representation of a number
              }
              }
              [/code]

              kind regards,

              Jos

              Comment

              • javaBookWorm
                New Member
                • Nov 2007
                • 10

                #8
                Originally posted by JosAH
                Suppose you have a String 's' and you want to convert it to an integer; you don't
                know whether the content of 's' makes sense as an int number; you could do this:

                [code=java]
                public int asInteger(Strin g s) {
                try {
                return Integer.parseIn t(s); // hope for the best
                }
                catch (NumberFormatEx ception nfe) {
                // 's' wasn't a representation of a number
                }
                }
                [/code]

                kind regards,

                Jos

                Jos,
                In my code where should i write this coding. Anyway my friend thought me something, before i asked the user to select the type of seats, an if statements was used. The if statement is like first i check whether the user key in an number or not if is not a number then error message out. But this manner was not really good.

                Would you be able to write the parse.Int(S) code for me, i mean where should i write such thing in my coding. Would you write it in my coding, so at least i get an picture of what it mean.

                Also, what happen if i wan to use throw and catch method, how to write it in my coding.can u write it in it and show me.

                I am sorry that i too demanding, cause i really want to master my Java. thank you Josh..

                Best Regard
                Junior Java Programmer

                Comment

                • javaBookWorm
                  New Member
                  • Nov 2007
                  • 10

                  #9
                  Originally posted by Ganon11
                  I wouldn't worry about regular expressions right now - using Exception handling is a lot easier. Here's a quick tutorial for using expressions.
                  Gannon11,
                  I look at the tutorial, i only can understand 40% out of it. When it come to my coding i don;t really know how to apply it. can you show me your ways in writting the throw and catch exception in my code, so i am able to get that picture of how it is used.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    You have to write that little snippet in your 'chooseType' method; that's the place
                    where you want the user to type a 1 or a 2. You obtain a String value from the
                    option dialog you're using, so maybe an additional do ... while(<not correct>);
                    thingie might be of help as well.

                    kind regards,

                    Jos

                    Comment

                    • javaBookWorm
                      New Member
                      • Nov 2007
                      • 10

                      #11
                      Originally posted by JosAH
                      You have to write that little snippet in your 'chooseType' method; that's the place
                      where you want the user to type a 1 or a 2. You obtain a String value from the
                      option dialog you're using, so maybe an additional do ... while(<not correct>);
                      thingie might be of help as well.

                      kind regards,

                      Jos
                      Thank Jos for your help. The snippet is the one you send to me earlier one right. Hehe. I think i know what to do. In the do while statement, i will write it as when user key in 1 then selectSeat(seat Type); to allow the user to select the seats when the integer is true. Where else the while statements will give an errors message. Hehe


                      I really wanna thank for all your patient in teaching me to a good students in Java.Thank you

                      Joseph

                      Comment

                      • javaBookWorm
                        New Member
                        • Nov 2007
                        • 10

                        #12
                        Originally posted by javaBookWorm
                        Thank Jos for your help. The snippet is the one you send to me earlier one right. Hehe. I think i know what to do. In the do while statement, i will write it as when user key in 1 then selectSeat(seat Type); to allow the user to select the seats when the integer is true. Where else the while statements will give an errors message. Hehe


                        I really wanna thank for all your patient in teaching me to a good students in Java.Thank you

                        Joseph
                        I try to type like that

                        public void chooseType asInteger (String s) {

                        }
                        when i compile it,it say that an error occur where there is no (

                        what it mean..and how to correct it

                        Comment

                        • JosAH
                          Recognized Expert MVP
                          • Mar 2007
                          • 11453

                          #13
                          Originally posted by javaBookWorm
                          TI really wanna thank for all your patient in teaching me to a good students in Java.Thank you

                          Joseph
                          I guess that you can manage from here; and you're welcome of course. Think of
                          methods as little tools. You supply them with the information they need and they
                          return the value that you want. If you can't describe such a method in detail in just
                          one or a few sentences you can be sure that your method will be too complicated.
                          You need to 'decompose' your method in to small methods then until you can
                          describe each little method in detail in just at most a few sentences.

                          There's nothing magic about it: it's just an old rule of thumb already known in the
                          age of structured programming.

                          Your method should contain a while or do ... while loop, catch the optional exception
                          that's thrown by the Integer.parseIn t() method. If an exception was thrown the
                          user certainly goofed so try again; otherwise return the parsed integer value.

                          kind regards,

                          Jos

                          Comment

                          Working...