4 Replies - 406 Views - Last Post: 16 May 2012 - 08:22 PM Rate Topic: -----

#1 kiubo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 12

logical error in validating a text Fields

Posted 16 May 2012 - 03:33 AM

hi evey one

I have 3 text fields are First Name ,Last Name and password.
First Name ,Last Name are must be characters,No empty values

password is accept all digits or character is must to equal 8

I do it but the I faced a logical error that is accept number values in First Name ,Last Name

    private void save_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            

        
        AdminCombobox = !buttonGroup1.isSelected(Admin_RadioButton.getModel());
        SWCombobox = !buttonGroup1.isSelected(Social_RadioButton.getModel());
        System.out.print(!buttonGroup1.isSelected(Admin_RadioButton.getModel()));
        studentId = userID_Field.getText();
        studentFirst = firstNameUser_TextField.getText();
        studentLast = lasttNameUser_TextField.getText();

        studentPass = password_Field1.getText();
 if ( studentFirst.isEmpty() || studentLast.isEmpty() || studentPass.isEmpty()
 || AdminCombobox || SWCombobox) {//work fine
  
 if (studentFirst.isEmpty()) {//work fine
 JOptionPane.showMessageDialog(this, "First name field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
    firstNameUser_TextField.setText("");
         }
 if (studentLast.isEmpty()) {//work fine
 JOptionPane.showMessageDialog(this, "Last name field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
lasttNameUser_TextField.setText("");
         }
 if (studentPass.isEmpty()) {//work fine
 JOptionPane.showMessageDialog(this, "Password field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
 password_Field1.setText("");
             }
  if ((AdminCombobox) && (SWCombobox)) {//work fine
  JOptionPane.showMessageDialog(this, "you did not choses a type of user!!", "ERROR", JOptionPane.ERROR_MESSAGE);
             }
  if (studentPass.length() != 8 && !studentPass.isEmpty()) {
      
 JOptionPane.showMessageDialog(this, "length of password is not correct it must to be 8 digits or characters !!", "ERROR", JOptionPane.ERROR_MESSAGE);
                 userID_Field.setText("");
          
  }
  
  else{if (studentPass.length() == 8){
                if (!containsOnlyNumbers(studentFirst)&&!containsOnlyNumbers(studentLast))//error
                         
addUser();//to add new user
 JOptionPane.showMessageDialog(this, "OK  !!", "ERROR", JOptionPane.ERROR_MESSAGE);//just to if that correct or no
                
                }else{
  JOptionPane.showMessageDialog(this, "First name or last name is  must to be character!!", "ERROR", JOptionPane.ERROR_MESSAGE);
                 userID_Field.setText("");
  }
 }      }     
 
  
 }   



the method I think the problem start from here,

   public boolean containsOnlyNumbers(String str) {
        
 
        
        for (int i = 0; i < str.length(); i++) {

            if (!Character.isDigit(str.charAt(i)))
                return false;
        }
        
        return true;
    }



Is This A Good Question/Topic? 0
  • +

Replies To: logical error in validating a text Fields

#2 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,194
  • Joined: 06-March 08

Re: logical error in validating a text Fields

Posted 16 May 2012 - 03:56 AM

Really but really complicated code for nothing... but any how



  else{if (studentPass.length() == 8){

     if (!containsOnlyNumbers(studentFirst)&&!containsOnlyNumbers(studentLast))//error



first I guess it should be studentPass.length() >= 8 or you just accept passwords having exactly 8 characters ?

constainsOnlyNumber() shuld be applied to studentPass not studentFirst or studentLast no ???
Was This Post Helpful? 0
  • +
  • -

#3 kiubo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 12

Re: logical error in validating a text Fields

Posted 16 May 2012 - 04:35 AM

I used this method for studentFirst and studentLast,just to know it contains only characters..

Quote

first I guess it should be studentPass.length() >= 8 or you just accept passwords having exactly 8 characters ?


this is not case, the problem on accept studentFirst and studentLast a numeric values ..

anyway thanks for reading my code
Was This Post Helpful? 0
  • +
  • -

#4 kiubo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 12

Re: logical error in validating a text Fields

Posted 16 May 2012 - 01:27 PM

please I want to help me ..
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is online

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8032
  • View blog
  • Posts: 31,194
  • Joined: 06-March 08

Re: logical error in validating a text Fields

Posted 16 May 2012 - 08:22 PM

first indent your code correctly, it is impossible to read
	private void save_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            


		AdminCombobox = !buttonGroup1.isSelected(Admin_RadioButton.getModel());
		SWCombobox = !buttonGroup1.isSelected(Social_RadioButton.getModel());
		System.out.print(!buttonGroup1.isSelected(Admin_RadioButton.getModel()));
		studentId = userID_Field.getText();
		studentFirst = firstNameUser_TextField.getText();
		studentLast = lasttNameUser_TextField.getText();

		studentPass = password_Field1.getText();
		if ( studentFirst.isEmpty() || studentLast.isEmpty() || studentPass.isEmpty()
				|| AdminCombobox || SWCombobox) {//work fine

			if (studentFirst.isEmpty()) {//work fine
				JOptionPane.showMessageDialog(this, "First name field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
				firstNameUser_TextField.setText("");
			}
			if (studentLast.isEmpty()) {//work fine
				JOptionPane.showMessageDialog(this, "Last name field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
				lasttNameUser_TextField.setText("");
			}
			if (studentPass.isEmpty()) {//work fine
				JOptionPane.showMessageDialog(this, "Password field is empty!!", "ERROR", JOptionPane.ERROR_MESSAGE);
				password_Field1.setText("");
			}
			if ((AdminCombobox) && (SWCombobox)) {//work fine
				JOptionPane.showMessageDialog(this, "you did not choses a type of user!!", "ERROR", JOptionPane.ERROR_MESSAGE);
			}
			if (studentPass.length() != 8 && !studentPass.isEmpty()) {

				JOptionPane.showMessageDialog(this, "length of password is not correct it must to be 8 digits or characters !!", "ERROR", JOptionPane.ERROR_MESSAGE);
				userID_Field.setText("");

			}

			else{if (studentPass.length() == 8){
				if (!containsOnlyNumbers(studentFirst)&&!containsOnlyNumbers(studentLast))//error

					addUser();//to add new user
				JOptionPane.showMessageDialog(this, "OK  !!", "ERROR", JOptionPane.ERROR_MESSAGE);//just to if that correct or no

			}else{
				JOptionPane.showMessageDialog(this, "First name or last name is  must to be character!!", "ERROR", JOptionPane.ERROR_MESSAGE);
				userID_Field.setText("");
			}
			}  
		}     
	}


I would take a complete different approach.
If a condition make that you won't save the data, no reason to continue to just return; instead of stacking conditions one over the other

if(firstName not valid)
   display error
   return

if(lastName not valid)
   display error
   return

if(password not valid)
   display error
   return



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1