?
Solved

help me! sorry about that

Posted on 2005-04-24
8
Medium Priority
?
196 Views
Last Modified: 2010-03-31
My code

===================================
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/*<APPLET CODE="CustomerDetail" WIDTH="500" HEIGHT="300"></APPLET> */
public class CustomerDetail extends Applet implements ActionListener
{
      Label lb1, lb2, lb3, lb4, lb5,lb6, lb7;
      TextField id, name, address, phone, email;
      Choice sex;
      Button bt1,bt2;
      GridBagLayout gb;
      GridBagConstraints gbc;
public void init()
{
      gb = new GridBagLayout();
      setLayout(gb);
      gbc= new GridBagConstraints();
      lb1 = new Label("Customer Detail");
      lb2 = new Label("Student ID");
      lb3 = new Label("Student Name");
      lb4 = new Label("Sex");
      lb5 = new Label("Address");
      lb6 = new Label("Phone");
      lb7 = new Label("Email");
      
      id = new TextField(20);
      name = new TextField(20);
      address = new TextField(20);
      phone = new TextField(20);
      email = new TextField(20);
      
      sex = new Choice();
      sex.addItem("Male");
      sex.addItem("FerMale");
      
      bt1 = new Button("Save");
      bt2 = new Button("Reset");
      
      addComponent(lb1,0,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb2,1,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(id,1,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb3,2,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(name,2,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb4,3,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(sex,3,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb5,4,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(address,4,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb6,5,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(phone,5,1,1,2,GridBagConstraints.HORIZONTAL);
      addComponent(lb7,6,0,1,1,GridBagConstraints.HORIZONTAL);
      addComponent(email,6,1,1,2,GridBagConstraints.HORIZONTAL);
      Panel p = new Panel();
      p.add(bt1);
      p.add(bt2);
      addComponent(p,7,0,1,1,GridBagConstraints.HORIZONTAL);
      gbc.anchor = GridBagConstraints.WEST;
      Font f = new Font("arial",Font.BOLD,13);
      lb2.setFont(f);
    lb3.setFont(f);
    lb4.setFont(f);
    lb5.setFont(f);
    lb6.setFont(f);
    lb7.setFont(f);
    bt1.setFont(f);
    bt2.setFont(f);
    Font f1 = new Font("arial",Font.BOLD, 16);
    lb1.setFont(f1);
    id.addActionListener(this);
    name.addActionListener(this);
    phone.addActionListener(this);
    email.addActionListener(this);
    bt1.addActionListener(this);
    bt2.addActionListener(this);  
}
public void addComponent(Component c, int row, int col, int nrow, int ncol, int fill)
{
      gbc.gridx = col;
      gbc.gridy = row;
      gbc.gridwidth = ncol;
      gbc.gridheight = nrow;
      gbc.fill = fill;
      gb.setConstraints(c,gbc);
      add(c);
}
public void actionPerformed (ActionEvent e)
{
        
      if(e.getSource() == bt2)
      {
            id.setText("");
            name.setText("");
            address.setText("");
            phone.setText("");
            email.setText("");
            showStatus("Reset");
      }
      if(e.getSource() == bt1)
      {
      if(id.getText().equals(""))
      {
      showStatus("Student ID field must be fill!!");
      id.requestFocus();
      }
    else if(name.getText().equals(""))
      {
      showStatus("Student Name field must be fill!!");
      name.requestFocus();
      }

}
}
}
==========================================
hix, i dunt know how to set: email must follow the pattern 'useid@company.com'
+ phone must be number
+ when user click on Save button all data must be save to "text.txt" file
0
Comment
Question by:J3n
  • 4
6 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 13856110
Hi,

1." email must follow the pattern 'useid@company.com'". i think that you can check it using instr method of String.
2. "phone must be number". I can give you some sample after this.
3. I will show you how to save data into text file.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13856123
Hi,

1.
String string = "userid@company.com";
b = string.indexOf("@") > 0;

if (b == false)
{
// Wrong email
}
else
{
// Do your logic here
}

URL:http://www.javaalmanac.com/egs/java.lang/HasSubstr.html

2.
You can use Integer.parseInt to check it.

try
{
int phone = Integer.parseInt("123123");
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("Phone must be number");
}

3.
http://www.devdaily.com/java/edu/pj/pj010017/index.shtml

I hope that helps.

Regards
Dave
0
 

Author Comment

by:J3n
ID: 13856144
thanks much
but i still error, can you write complete code for me
Thanks again
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Accepted Solution

by:
suprapto45 earned 1000 total points
ID: 13856163
Hi,

Kind of this but I have not checked that yet.

       if(e.getSource() == bt1)
       {             
             if(id.getText().equals(""))
             {
                  System.out.println("Student ID field must be fill!!");
             id.requestFocus();
             }
            else if(name.getText().equals(""))
             {
                  System.out.println("Student Name field must be fill!!");
             name.requestFocus();
             }
             else if((email.getText()).indexOf('@') >0)
             {
                  System.out.println("Email must have @!!");
             }
            else
            {
            
                   try
                   {                         
                         int a = Integer.parseInt(phone.getText());
                         BufferedWriter out = new BufferedWriter(new FileWriter("details.txt"));
                         out.write(id.getText());
                        out.write(name.getText());
                        out.write(address.getText());
                        out.write(phone.getText());
                        out.write(email.getText());
                         out.close();
                   }
                   catch(NumberFormatException num)
                   {
                         System.out.println("Error, phone must be number");
                   }
                   catch (IOException es)
                   {
                   }
            }


}

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13856165
Hi,

Sorry.

             else if((email.getText()).indexOf('@') < 0)
             {                   
                  System.out.println("Email must have @!!");
             }

Regards
Dave
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 1000 total points
ID: 13856857
You need to accumulate error messages - just showing one in the status will only tell the user about one error condition. You can accumulate in a StringBuffer and then use JOptionPane.showMessage.

Also it would be easier to use a regular expression to check the email address
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question