The Artima Developer Community
Sponsored Link

Java Answers Forum
Help Please

1 reply on 1 page. Most recent reply: Jun 14, 2004 3:51 PM by Kishori Sharan

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
Nate Hultman

Posts: 8
Nickname: vtgunner14
Registered: May, 2004

Help Please Posted: Jun 14, 2004 10:52 AM
Reply to this message Reply
Advertisement
Hi, I posted the registration form question last night, but I have figured most of it out on my own. I was wondering if someone could please help me with one method that i need to get to work as well as a way to show a label if there arent any errors.

The method that i need help with is a zip code check. The zip has to be greater than or equal to 10000 and less than or equal to 20000.

The label part that im having troubles with is if a user enters all the info in the form without any errors, "thank you" will appear in a label. If there are errors, the label would say something like "please fix the areas that are red."

Here is my code and everything works correctly except those 2 problems stated above.

package program2;
 
// This program creates a small online form
// Designed by Nate Hultman
 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
 
public class Program2 extends Applet implements ActionListener
{
        // These are global objects for textfields, labels, and buttons
        private TextField nameBox = new TextField("",25); // room for 25 chars and empty
        private TextField addressBox = new TextField("",25);
        private TextField cityBox  = new TextField("",25);
        private TextField stateBox  = new TextField("",2);
        private TextField zipBox  = new TextField("",10);
        private TextField phoneBox  = new TextField("",15);
        private TextField emailBox  = new TextField("",25);
        private TextField productIDBox  = new TextField("",15);
 
        Label nameLabel = new Label ("Name:") ;
        Label addressLabel = new Label ("Address:");
        Label cityLabel = new Label ("City:");
        Label stateLabel = new Label ("State:");
        Label zipLabel = new Label ("Zip Code:");
        Label phoneLabel = new Label ("Phone:");
        Label emailLabel = new Label ("Email:");
        Label productIDLabel = new Label ("Product ID:");
 
        private Button submit = new Button ("Submit");
 
        public void init()
        {
          setBackground(Color.blue);
                //layout for overall applet is GridLayout with 9 rows, 2 columns
                setLayout(new GridLayout(9,1));
 
                // first row
                add(nameLabel);
                             add(nameBox);
 
                // second row
                add(addressLabel );
                             add(addressBox);
 
                // third row
                add(cityLabel);
                             add(cityBox);
 
                // fourth row
                add(stateLabel );
                             add(stateBox);
 
                // fifth row
                add(zipLabel );
                             add(zipBox);
 
                // sixth row
                add(phoneLabel );
                             add(phoneBox);
 
                // seventh row
                add(emailLabel);
                             add(emailBox);
 
                // eighth row
                add(productIDLabel);
                             add(productIDBox);
 
                // ninth row
                add(submit);
                             submit.addActionListener(this);
        }// ends init method
 
        public void actionPerformed( ActionEvent e )
        {                    //Declaring all the methods
           checkName ();
 
           checkAddress ();
 
           checkCity ();
 
           checkState();
 
           checkZip ();
 
           checkPhone();
 
           checkEmail();
 
           checkLabel();
        }
 
        //All the error checking methods
 
        public void checkName ( )
        {
              String name = nameBox.getText();
              if (name.length() == 0)        //checks lenght is at least one character
                   nameLabel.setForeground(Color.red);
              else
                   nameLabel.setForeground(Color.yellow);
        }
 
        public void checkAddress(){
 
              String address = addressBox.getText();
              if (address.length() < 4)        //checks lenght is at least one character
                   addressLabel.setForeground(Color.red);
              else
                   addressLabel.setForeground(Color.yellow);
        }
 
        public void checkCity(){
 
              String city = cityBox.getText();
              if (city.length() == 0)        //checks lenght is at least one character
                   cityLabel.setForeground(Color.red);
              else
                   cityLabel.setForeground(Color.yellow);
        }
 
        public void checkState(){
 
              String state = stateBox.getText();
              if  (state.equals("PA") || state.equals("NY")
                   || state.equals("pa") || state.equals("ny"))
                    stateLabel.setForeground(Color.yellow);
              else
                  stateLabel.setForeground(Color.red);
        }
 
        
        
        //PLEASE HELP ME WITH THIS PART
        public void checkZip(){
          
              String zip = zipBox.getText();
              if (zip.startsWith("0") || zip.equals(zip => '20001'))
                zipLabel.setForeground(Color.red);
              else
                zipLabel.setForeground(Color.yellow);
          }
 
 
        public void checkPhone(){
 
          String phone = phoneBox.getText();
              if (phone.startsWith("702") || phone.startsWith("412"))        //checks lenght is at least one character
                  phoneLabel.setForeground(Color.yellow);
              else
                  phoneLabel.setForeground(Color.red);
 
        }
 
        public void checkEmail(){
 
                String email   =    emailBox.getText( );     // gets the text from the email box
                if   (email.indexOf ( '@' )   ==  -1  )      // -1 means no occurence of @ sign in email
                        emailLabel.setForeground(Color.red);
                else
                        emailLabel.setForeground(Color.yellow);
        }
 
        
        //PLEASE HELP ME WITH THIS
        public void checkLabel(){
 
 
        }
 
 
 
} // end program
 


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Help Please Posted: Jun 14, 2004 3:51 PM
Reply to this message Reply
Here is the code for your checkZip () method.
public void checkZip(){
 
              String zip = zipBox.getText();
              int zipNum = 0 ;
              // Make sure ZIP is a number
              try {
                zipNum = Integer.parseInt(zip);
                if ( zipNum >= 10000 && zipNum <= 20000 ) {
                  // Zip is ok
                  zipLabel.setForeground(Color.yellow);
                }
                else {
                  // Zip is not in range
                  zipLabel.setForeground(Color.red);
                }
              }
              catch (Exception e ) {
                // Zip entered is not a number
                // Do what you want to do in this error condition
                zipLabel.setForeground(Color.red);
              }
 
          }
 

Flat View: This topic has 1 reply on 1 page
Topic: need help in integrating java w/ sql server 2000 reporting services Previous Topic   Next Topic Topic: Blojsom and news/RSS feeds?

Sponsored Links



Google
  Web Artima.com   

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